Created
December 17, 2010 01:26
-
-
Save elidupuis/744331 to your computer and use it in GitHub Desktop.
A base structure to use when building new plugins!
This file contains hidden or 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($) { | |
var methods = { | |
init: function( options ) { | |
// iterate and reformat each matched element | |
return this.each(function() { | |
var $this = $(this), | |
opts = $.extend({}, $.fn.PLUGIN_NAME.defaults, options), | |
data = $this.data('PLUGIN_NAME'); | |
// If the plugin hasn't been initialized yet | |
if ( ! data ) { | |
// do all your main awesomeness in here... | |
// attach | |
$this.data('PLUGIN_NAME', { | |
target : $this, | |
opts: opts | |
}); | |
}; | |
}); | |
}, | |
update: function() { | |
// each method must loop through all selected elements and return 'this'. | |
return this.each(function() { | |
if(window.console) window.console.log('update called.'); | |
}); | |
} | |
}; | |
// main plugin declaration: | |
$.fn.PLUGIN_NAME = function( method ) { | |
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 jQuery.PLUGIN_NAME' ); | |
}; | |
}; | |
// defaults | |
$.fn.PLUGIN_NAME.defaults = { | |
optionA: true | |
// etc... | |
}; | |
$.fn.PLUGIN_NAME.publicfunc = function() { return "jquery.PLUGIN_NAME public function. "; }; | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment