-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackbone.module.js
150 lines (121 loc) · 3.36 KB
/
backbone.module.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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
(function() {
'use strict';
var module = Backbone.Module = {};
module.namespaces = {};
var wrapTail = function(func, wrapper) {
var arg_count = wrapper.length -1;
return function() {
var args = Array.prototype.slice.call(arguments);
if(args.length < arg_count){
for ( var i = 0; i < arg_count - args.length ; i++) {
args.push(undefined);
}
}
args.push(func);
return wrapper.apply(this, args);
};
};
module.wrap = function() {//_.extend event, rout,fixedparam, wrapTail function
var ret = this._getResult();
for ( var i = 0; i < arguments.length; i++) {
var argument = arguments[i];
var mod = this.getModule(argument);
for ( var name in mod) {
var prop = mod[name];
if (this._extendable(prop) && (_.isUndefined(ret[name]) === true || this._extendable(ret[name]))) {
ret[name] = _.extend(ret[name] || {},prop);
}
else
{
if(_.isUndefined(ret,name) !== true){
if(typeof ret[name] === 'function'){
ret[name] = wrapTail(ret[name],prop);
}
else
{
ret[name] = prop;
}
}
else
{
ret[name] = prop;
}
}
}
}
return ret;
};
module._extendable = function(prop) {
return _.isObject(prop) === true &&_.isFunction(prop) === false &&_.isArray(prop) === false;
}
//extend Backbone basic class
module.extendView = function() {
var modules = Array.prototype.slice.call(arguments);
return this.extendTo(Backbone.View, modules);
};
module.extendModel = function() {
var modules = Array.prototype.slice.call(arguments);
return this.extendTo(Backbone.Model, modules);
};
module.extendCollection = function() {
var modules = Array.prototype.slice.call(arguments);
return this.extendTo(Backbone.Collection, modules);
};
module.extendRouter = function(){
var modules = Array.prototype.slice.call(arguments);
return this.extendTo(Backbone.Model, modules);
};
module.extendTo = function(target,modules) {
if(_.isArray(modules) === false){
modules = Array.prototype.slice.call(arguments);
modules.shift();
}
var ex = this.wrap.apply(this, modules);
return target.extend(ex);
}
//regist module
module.regist = function(namespace,mod){//regist module
this._registNameSpace(namespace, mod);
return mod;
};
module.registWrap = function(namespace) {//regist with extend
var args = Array.prototype.slice.call(arguments,1);
var mod = this.wrap.apply(this, args);
this._registNameSpace(namespace, mod);
return mod;
};
module._registNameSpace = function(namespace,mod){
var names = namespace.split(".");
var regist_name = names.pop();
var target = module.namespaces;
for ( var i = 0; i < names.length; i++) {
var name = names[i];
if(_.has(target,name) === false){
target[name] = {};
}
target = target[name];
}
target[regist_name] = mod;
};
module.getModule =function(param){
if(_.isString(param) === false){
return param;
}
else
{
var names = param.split(".");
var ret = module.namespaces;
for ( var i = 0; i < names.length; i++) {
var name = names[i];
if(_.has(ret,name) === false){
ret[name] = {};
}
ret = ret[name];
}
return ret;
}
};
module._getResult = function() {
return _.extend({},this);
};
})();