Skip to content

Instantly share code, notes, and snippets.

@garystorey
Last active August 29, 2015 14:03
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 garystorey/025e37b559d6d13ef34e to your computer and use it in GitHub Desktop.
Save garystorey/025e37b559d6d13ef34e to your computer and use it in GitHub Desktop.
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