Created
October 29, 2012 15:00
-
-
Save yohang/3974023 to your computer and use it in GitHub Desktop.
jQuery pluginize a Javascript object
This file contains 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
/*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