Created
September 24, 2012 01:28
-
-
Save mmazer/3773729 to your computer and use it in GitHub Desktop.
jQuery: Plugin template
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 (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