Skip to content

Instantly share code, notes, and snippets.

@morningtoast
Last active August 29, 2015 14:01
Show Gist options
  • Save morningtoast/247cd036a8412c94867e to your computer and use it in GitHub Desktop.
Save morningtoast/247cd036a8412c94867e to your computer and use it in GitHub Desktop.
Updated jQuery plugin template
/*
My Plugin
The description/summary of the plugin
5/21/14 ~BV
Syntax:
$(selector).myPlugin(options)
Usage:
$(".example").myPlugin();
Options:
Options should be passed as an object
configOption2 obj Definition and options for this parameter
configOption1 str Definition and options for this parameter
debug bool When TRUE outputs debugging info to console [false]
Example:
$(".example").myPlugin({
configOption1: "something",
configOption2: function(e) {
alert("Something");
},
debug: true
});
*/
(function($){
$.PLUGIN_NAME = function(loopEl, options){
var loopObj = $(loopEl);
// Plugin config, local helpers
var plugin = {
settings: {},
init: function() {
plugin.settings = $.extend({},$.PLUGIN_NAME.defaultOptions, options); // Merge settings
},
debug: function(s) {
if (plugin.settings.debug) { console.log(s); }
}
}
// Plugin actions, data handling
var actions = {
myPluginFunction: function(m) {
loopObj.html(m);
plugin.debug(plugin.settings);
// Do something with data
}
}
// Init plugin, runs before
plugin.init();
return(actions);
};
// Default options for plugin, overwrite with passed object
$.PLUGIN_NAME.defaultOptions = {
debug: true
};
// Define plugin namespace
// Replace PLUGIN_NAME with what you want your plugin called
$.fn.PLUGIN_NAME = function(options){
return this.each(function(){
// The Loop
var plugin = (new $.PLUGIN_NAME(this, options));
plugin.myPluginFunction("bar");
// Binding
// plugin.bind();
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment