Skip to content

Instantly share code, notes, and snippets.

@ganglio
Created July 11, 2012 12:40
Show Gist options
  • Save ganglio/3090119 to your computer and use it in GitHub Desktop.
Save ganglio/3090119 to your computer and use it in GitHub Desktop.
jQuery: Plugin Skeleton
(function($){
$.plugin = $.plugin || {version: "1.0.0"};
$.plugin.defaultConf = {
conf : {
}
};
function Plugin(elem, conf) {
var
self = this,
fire = elem.add(self);
// API methods
$.extend(self,{
// Runs the app
f1: function(e) {
},
// returns the running status
f2: function() {
},
// Run related events
e1: function(e){},
e2: function(e){},
});
// callbacks
$.each("e1,e2".split(","), function(i, name) {
// configuration
if ($.isFunction(conf[name])) {
$(self).bind(name, conf[name]);
}
});
// Default behaviour
self.f1();
}
// jQuery plugin initialization
$.fn.Plugin = function(conf) {
// already constructed --> return API
var el = this.data("plugin-data");
if (el) { return el; }
conf = $.extend(true, $.plugin.defaultConf.conf, {}, conf);
this.each(function() {
el = new Plugin($(this), conf);
$(this).data("plugin-data", el);
});
return el;
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment