jQuery boilerplate
by Shishir Morshed
JavaScript
;(function($){
function plugin(elem, options){
var self = this;
self.$elem = $(elem);
var defaults ={
msg : "you clicked me!"
};
self.options =$.extend({}, defaults, options);
self.count = 1;
init();
function init(){
self.$elem.click( clickHandler );
}
function clickHandler(event){
alert( self.options.msg +" "+self.count );
self.count += 1;
}
function changMsg(msg){
self.options.msg = msg;
}
var API = {};
API.changeMsg = changeMsg;
return API;
}
$.fn.plugin = function(options){
return this.each(function(){
if(!&(this).data("plugin")){
$(this).data("plugin", new plugin(this, options));
}
});
};
})(jQuery);
var options = { msg : "click" };
$("#elm").plugin(options);
var api =$("#elm").data("plugin");
api.changeMsg("hello there!");