-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathadvanced-jquery-boilerplate.js
60 lines (42 loc) · 1.36 KB
/
advanced-jquery-boilerplate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**!
* Advanced jQuery Plugin Boilerplate
* @author: Cedric Ruiz
* https://github.com/elclanrs/advanced-jquery-boilerplate
*/
(function($) {
var AP = Array.prototype;
$.newPlugin = function(pluginName, defaults, methods, global) {
function Plugin(element, options) {
this.opts = $.extend({}, defaults, options);
this.el = element;
this._name = pluginName;
this._init();
}
Plugin.prototype._init = $.noop;
Plugin.prototype[pluginName] = function(method) {
if (!method) return this;
try { return this[method].apply(this, AP.slice.call(arguments, 1)); }
catch(e) {}
};
$.extend(Plugin.prototype, methods);
if (global) $[pluginName] = global;
$.fn[pluginName] = function() {
var args = AP.slice.call(arguments)
, method = typeof args[0] == 'string' && args[0].split(':')
, opts = typeof args[0] == 'object' && args[0]
, params = args.slice(1)
, isGet = method[0] == 'get'
, ret;
this.each(function() {
var instance = $.data(this, pluginName);
// Method
if (instance) {
return ret = instance[pluginName].apply(instance, [method[+isGet]].concat(params));
}
// Init
return $.data(this, pluginName, new Plugin(this, opts));
});
return isGet ? ret : this;
};
};
}(jQuery));