Skip to content

Instantly share code, notes, and snippets.

@mmazer
Created September 24, 2012 01:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mmazer/3773729 to your computer and use it in GitHub Desktop.
Save mmazer/3773729 to your computer and use it in GitHub Desktop.
jQuery: Plugin template
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(/*==@id==*/['jquery'], factory);
} else {
factory(jQuery);
}
})(function ($) {
var _defaults = { }
function Plugin(elem, options) {
if ( elem ) {
this.init(elem, options);
}
}
$.extend(Plugin.prototype, {
name: "modal",
init: function(el, options) {
var self = this;
$.data(el, this.name, this);
this.settings = $.extend({}, $.fn[this.name].options, options);
this.data = { elem: $(el) };
},
destroy: function() { }
// other Plugin API methods
});
$.fn[Plugin.prototype.name] = function (options) {
return this.each(function() {
var plugin = $.data(this, Plugin.prototype.name);
if (plugin) {
if ( typeof options === 'string') {
plugin[options].apply(plugin, Array.prototype.slice.call( arguments, 1));
}
}else {
plugin = new Plugin(this, options);
}
});
};
$.fn[Plugin.prototype.name].options = $.extend({}, _defaults);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment