Skip to content

Instantly share code, notes, and snippets.

@barendb
Created October 21, 2014 19:44
Show Gist options
  • Save barendb/2c91e1017b36fe4b2d20 to your computer and use it in GitHub Desktop.
Save barendb/2c91e1017b36fe4b2d20 to your computer and use it in GitHub Desktop.
Tried and trusted plugin boilerplate for jQuery
// http://stefangabos.ro/jquery/jquery-plugin-boilerplate-revisited/
(function($) {
$.pluginName = function(element, options) {
var defaults = {
foo: 'bar',
onFoo: function() {}
};
var plugin = this;
plugin.settings = {};
var $element = $(element);
plugin.init = function() {
plugin.settings = $.extend({}, defaults, options);
// code goes here
};
plugin.foo_public_method = function() {
// code goes here
};
var foo_private_method = function() {
// code goes here
};
plugin.init();
};
$.fn.pluginName = function(options) {
return this.each(function() {
if(undefined === $(this).data('pluginName')) {
var plugin = new $.pluginName(this, options);
$(this).data('pluginName', plugin);
}
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment