Skip to content

Instantly share code, notes, and snippets.

@oscarotero
Last active December 20, 2015 16:49
Show Gist options
  • Save oscarotero/6163869 to your computer and use it in GitHub Desktop.
Save oscarotero/6163869 to your computer and use it in GitHub Desktop.
jQuery plugin boilerplate
;(function ($, window, document, undefined) {
var pluginName = "name", defaults = {};
function Plugin (element, options) {
this.element = element;
this.settings = $.extend({}, defaults, options);
this.init();
}
Plugin.prototype = {
init: function () {
}
};
$.fn[pluginName] = function (options) {
if ((options === undefined) || (typeof options === 'object')) {
return this.each(function () {
if (!$.data(this, "plugin_" + pluginName)) {
$.data(this, "plugin_" + pluginName, new Plugin(this, options));
}
});
}
if ((typeof options === 'string') && (options[0] !== '_') && (options !== 'init')) {
var returns, args = arguments;
this.each(function () {
var instance = $.data(this, 'plugin_' + pluginName);
if ((instance instanceof Plugin) && (typeof instance[options] === 'function')) {
returns = instance[options].apply(instance, Array.prototype.slice.call(args, 1));
}
if (options === 'destroy') {
$.data(this, 'plugin_' + pluginName, null);
}
});
return returns !== undefined ? returns : this;
}
};
})(jQuery, window, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment