forked from jupiterjs/jquerymx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.js
More file actions
93 lines (82 loc) · 2.48 KB
/
plugin.js
File metadata and controls
93 lines (82 loc) · 2.48 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
steal('jquery/controller/controller_core.js', function(){
/**
* @add jQuery.fn
*/
//used to determine if a controller instance is one of controllers
//controllers can be strings or classes
var i,
isAControllerOf = function( instance, controllers ) {
for ( i = 0; i < controllers.length; i++ ) {
if ( typeof controllers[i] == 'string' ? instance.constructor._shortName == controllers[i] : instance instanceof controllers[i] ) {
return true;
}
}
return false;
},
data = function(el, data){
return $.data(el, "controllers", data)
},
makeArray = $.makeArray;
$.fn.extend({
/**
* @function controllers
* Gets all controllers in the jQuery element.
* @return {Array} an array of controller instances.
*/
controllers: function() {
var controllerNames = makeArray(arguments),
instances = [],
controllers, c, cname;
//check if arguments
this.each(function() {
controllers = $.data(this, "controllers");
for ( cname in controllers ) {
if ( controllers.hasOwnProperty(cname) ) {
c = controllers[cname];
if (!controllerNames.length || isAControllerOf(c, controllerNames) ) {
instances.push(c);
}
}
}
});
return instances;
},
/**
* @function controller
* Gets a controller in the jQuery element. With no arguments, returns the first one found.
* @param {Object} controller (optional) if exists, the first controller instance with this class type will be returned.
* @return {jQuery.Controller} the first controller.
*/
controller: function( controller ) {
return this.controllers.apply(this, arguments)[0];
}
});
$.Controller.plugin = function(pluginname){
var controller = this;
if (!$.fn[pluginname]) {
$.fn[pluginname] = function(options){
var args = makeArray(arguments), //if the arg is a method on this controller
isMethod = typeof options == "string" && $.isFunction(controller.prototype[options]), meth = args[0];
return this.each(function(){
//check if created
var controllers = data(this), //plugin is actually the controller instance
plugin = controllers && controllers[pluginname];
if (plugin) {
if (isMethod) {
// call a method on the controller with the remaining args
plugin[meth].apply(plugin, args.slice(1));
}
else {
// call the plugin's update method
plugin.update.apply(plugin, args);
}
}
else {
//create a new controller instance
controller.newInstance.apply(controller, [this].concat(args));
}
});
};
}
}
});