Skip to content

Instantly share code, notes, and snippets.

@msrafi
Created May 29, 2015 21:43
Show Gist options
  • Save msrafi/ee59d0d42815be5ac01b to your computer and use it in GitHub Desktop.
Save msrafi/ee59d0d42815be5ac01b to your computer and use it in GitHub Desktop.
dynamical jquery plugin
// This is our NEWSAMPLE Component - Say Accordion - Lets put in the seperate file
var NewSample = {
init: function(elem, options){
var elem = $(elem);
// Some function for this accordion
if (options && typeof(options) == 'object') {
// extend the options here
console.log(options);
}
elem.css('background-color', '#f90');
}
};
// Return the object like
// return NewSample;
// Make this as another file
$.fn.attachJQ = function(obj, pluginName, options){
var $elem = $(this);
if($elem.length > 0){
$.fn[pluginName] = function(options){
return this.each(function(i, elem) {
elem[pluginName] = Object.create(obj);
elem[pluginName].init(this, options, i);
});
};
$elem[pluginName](options);
}
};
// All plugins return jsut jquery
// return $;
// This is how the plugins are called
// $(ELEMENT).attachJQ(OBJECT, NAMEOFPLUGIN, OPTIONS)
$('.template').attachJQ(NewSample, 'accordion');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment