-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto.js
103 lines (82 loc) · 2.18 KB
/
proto.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
var types = require("./types"),
is = require("istype"),
bindSubEvent = require("./bindSubEvent");
exports.set = function(attrs) {
if (this._instant) {
this.begin();
this.attrs = attrs;
this.end();
} else {
for (var k in attrs) {
this.attrs[k] = attrs[k];
}
}
return this;
};
exports.toJSON = function() {
var jsonObj = {};
var attrs = this.model.attrs;
for (var k in attrs) {
var type = attrs[k].type;
var v = this[k];
if (v) {
if (this.model.isComplexType(k)) {
jsonObj[k] = v.toJSON()
} else {
// DOTo
if (type === "date") {
jsonObj[k] = v.getTime();
} else if (type === "regexp") {
jsonObj[k] = v.toString();
} else {
jsonObj[k] = v;
}
}
} else {
jsonObj[k] = v;
}
}
return jsonObj;
}
exports.validate = function(attr_name) {
var keys = [];
if (is.type(attr_name) === "array") {
keys = attr_name;
} else if (is.type(attr_name) === "string") {
keys.push(attr_name);
} else {
keys = Object.keys(this.model.attrs);
}
var fns = this.model.validators;
for (var i = 0; i < fns.length; i++) {
fns[i](this, keys);
}
};
exports.begin = function() {
this._instant = false;
this.result.clearError();
}
exports.end = function() {
this.model.emit("changing", this, this.attrs);
this.emit("changing", this.attrs);
this.validate(Object.keys(this.attrs));
if (!this.hasError()) {
this._instant = true;
var data = this.attrs;
var names = [];
for (var k in data) {
var v = this.oattrs[k] = data[k];
if (this.model.isComplexType(k)) {
names.push(k);
}
}
bindSubEvent(this, names);
this.model.emit("changed", this, this.attrs);
this.emit("changed", this.attrs);
}
this.attrs = {};
this._instant = true;
}
exports.hasError = function() {
return this.result.hasError();
}