Skip to content

Instantly share code, notes, and snippets.

@yohang
Created October 29, 2012 15:00
Show Gist options
  • Save yohang/3974023 to your computer and use it in GitHub Desktop.
Save yohang/3974023 to your computer and use it in GitHub Desktop.
jQuery pluginize a Javascript object
/*global jQuery*/
(function($) {
'use strict';
$.pluginize = function(name, constructor) {
// Déclaration du plugin
$.fn[name] = function(option) {
var args = arguments;
return this.each(function() {
var $this = $(this);
var data = $this.data(name);
if (data === undefined) {
$this.data(name, data = new constructor($this, option));
}
if (typeof option === 'string' && typeof data[option] === 'function') {
data[option].apply(data, [].slice.call(args, 1));
}
});
};
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment