Skip to content

Instantly share code, notes, and snippets.

@romelperez
Last active November 22, 2016 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romelperez/de2de9a63c3fd33c2c3ae7a53e74f402 to your computer and use it in GitHub Desktop.
Save romelperez/de2de9a63c3fd33c2c3ae7a53e74f402 to your computer and use it in GitHub Desktop.
A jQuery plugin boilerplate
const NAME = 'pluginName';
const methods = {
method1: function () {
const conf = this.data(`${NAME}-config`);
if (!conf) return this;
//
return this;
}
};
jQuery[NAME] = {
CONSTANT1: '...',
globalMethod1: () => {
//
}
};
jQuery.fn[NAME] = function (settings) {
if (!this.length) return this;
if (typeof settings === 'string') {
if (methods[settings]) {
const args = Array.prototype.slice.call(arguments, 1);
return methods[settings].apply(this, args);
} else {
throw new Error(`${NAME} method unrecognized "${settings}".`);
}
}
if (this.data(`${NAME}-config`)) return;
const conf = $.extend(true, {
//
}, settings);
this.data(`${NAME}-config`, conf);
this.each(function () {
// HTML
//
// EVENTS
//
// INIT
//
});
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment