Last active
August 29, 2015 14:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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