-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatic.js
162 lines (130 loc) · 3.63 KB
/
static.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
151
152
153
154
155
156
157
158
159
160
161
162
var types = require("./types"),
is = require("istype"),
bindSubEvent = require("./bindSubEvent");
exports.attr = function(name, option) {
option = option || {};
var type = option.type;
if (type && is.type(type) === "function") {
if (!(type.prototype.toJSON && type.reborn)) {
throw new Error("custom type must implement toJSON & reborn");
}
} else if (!type) {
option.type = "string";
}
this.attrs[name] = option;
this.createAttr(name);
return this;
}
exports.toJSON = function(model, keys) {
var jsonObj = {};
keys.forEach(function(k) {
var v = model[k];
if (v) {
if (model.model.isComplexType(k)) {
jsonObj[k] = v.toJSON()
} else {
jsonObj[k] = v;
}
} else {
jsonObj[k] = v;
}
});
return jsonObj;
}
exports.getComplexAttrNames = function() {
var attrNames = [];
for (var k in this.attrs) {
type = this.attrs[k].type;
if (is.type(type) === "function") {
attrNames.push(k);
}
}
return attrNames;
}
exports.isComplexType = function(attrName) {
var type = this.attrs[attrName].type;
return is.type(type) === "function";
}
exports.createAttr = function(k) {
Object.defineProperty(this.prototype, k, {
get: function() {
var v = this.oattrs[k];
if (v) {
var type = this.model.attrs[k].type;
switch (type) {
case "regexp":
return new RegExp(v.toString());
break;
case "date":
var time = new Date();
time.setTime(v.getTime());
return time;
break;
case "array":
case "json":
try {
return JSON.parse(JSON.stringify(v));
} catch (e) {
return null;
}
break;
default:
return v;
break;
}
} else {
return v;
}
},
set: function(v) {
var o = {};
o[k] = v;
this.set(o);
}
})
}
exports.method = function(name, fn) {
if (!this.prototype[name]) {
this.prototype[name] = fn;
}
return this;
}
exports.use = function(plugin) {
plugin(this);
return this;
}
exports.validate = function(validator) {
this.validators.push(validator);
return this;
}
exports.reborn = function(jsonObj) {
var attrs = this.attrs;
var obj = new this();
var self = this;
obj.result.clearError();
var keys = Object.keys(this.attrs);
keys.forEach(function(k) {
var v = jsonObj[k];
if (types.indexOf(self.attrs[k].type) !== -1) {
var t = attrs[k].type;
if (t === "date") {
var time = new Date();
time.setTime(v);
obj.oattrs[k] = time;
} else if (t === "regexp") {
obj.oattrs[k] = new RegExp(v);
} else {
obj.oattrs[k] = v;
}
} else {
var type = attrs[k] ? attrs[k].type : null;
if (type) {
obj.oattrs[k] = type.reborn(v);
} else {
obj.oattrs[k] = v;
}
}
});
bindSubEvent(obj, this.getComplexAttrNames());
return obj;
}