Skip to content

Instantly share code, notes, and snippets.

@interfeis
Created July 2, 2012 16:32
Show Gist options
  • Save interfeis/3034121 to your computer and use it in GitHub Desktop.
Save interfeis/3034121 to your computer and use it in GitHub Desktop.
jQuery plugin boilerplate
(function($){
$.fn.extend({
//plugin name
pluginName: function(method) {
var methods = {
init : function(options) {
var defaults = {
color: '#333333'
//more default options
//...
};
//map defaults and options
var options = $.extend(defaults, options);
return this.each(function() {
var o = options,
obj = $(this),
$labels = $("label", obj);
//more cached selectors
//...
//apply options
$labels.css({'color': o.color});
//...
//more logic, animations, etc
//...
});//this.each
},//init
//more functions
//..
destroy : function( ) {
//destroy logic
}//destroy
};//methods
if ( methods[method] ) {
return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on this plugin' );
}
}//pluginName
});//$.fn.extend
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment