Skip to content

Instantly share code, notes, and snippets.

@coffindragger
Created October 30, 2011 21:29
Show Gist options
  • Save coffindragger/1326462 to your computer and use it in GitHub Desktop.
Save coffindragger/1326462 to your computer and use it in GitHub Desktop.
jQuery Plugin Template
(function($) {
var PLUGIN = "" //TODO: The name of the plugin
var methods = {
init: function (config) {
var options = {
//TODO: Default Options
}
$.extend(options, config)
return this.each(function() {
var $this = $(this),
data = $this.data(PLUGIN);
if (!data) {
data = {
//instance variables
target: $this,
};
$this.data(PLUGIN, data);
//TODO: Constructor
}
});
},
destroy: function() {
return this.each(function() {
var $this = $(this),
data = $this.data(PLUGIN);
//TODO: Destructor
// remove data
$this.removeData(PLUGIN);
//unbind namespaced events
$(window).unbind('.'+PLUGIN);
});
},
//TODO: Class Methods
};
$.fn[PLUGIN] = 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);
}
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment