Instantly share code, notes, and snippets.

Embed
What would you like to do?
Boilerplate jQuery Plugin - replace "Constructor" with constructor function name and replace "pluginName" with name of plugin
(function($, document, window, undefined) {
'use strict';
var Constructor = function( elm, options, id ) {
this.el = elm;
this.$el = $(elm);
this.init( options );
};
Constructor.prototype = {
init: function( options) {
this.options = $.extend( true, {}, $.fn.pluginName.defaults, options );
},
destroy : function ( ) {}
};
jQuery.fn.pluginName = function( options ) {
switch ( typeof options ) {
case 'number': //if the plugin acccepts a number. if not remove
Constructor = $(this).data( 'Constructor' );
if ( Constructor !== null ) {
//do something
}
break;
case 'string': // if the plugin accepts a string value (typically function name)
Constructor = $(this).data( 'Constructor' );
if ( Constructor !== null ) {
switch( options ) {
case 'init':
Constructor.init();
break;
case 'destroy':
Constructor.destroy();
break;
}
}
break;
default: // standard object
return this.each( function () {
new Constructor( $( this ), options );
});
}
};
jQuery.fn.pluginName.defaults = {
option1 : '',
option2 :0,
//etc
};
}(jQuery, document, window));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment