-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataModel.js
98 lines (81 loc) · 2.61 KB
/
DataModel.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
/*global define: true */
define([
"dojo/_base/declare",
"dojo/Stateful"
], function (
declare,
Stateful
) {
"use strict";
return declare([Stateful], {
props: {},
constructor: function (params) {
this.props = params.props || this.props;
this.initialize();
},
// load data from storage
deserialize: function (data) {
var deserializer = null, prop = null;
this.initialize();
for (prop in this.props) {
if (this.props.hasOwnProperty(prop)) {
if (data[prop] !== undefined) {
deserializer = this[prop + 'Deserializer'];
if (typeof deserializer === 'function') {
deserializer.apply(this, [data[prop]]);
} else {
this.set(prop, data[prop]);
}
}
}
}
},
// collect data for starage
serialize: function () {
var data = {}, serializer = null, prop = null;
for (prop in this.props) {
if (this.props.hasOwnProperty(prop)) {
serializer = this[prop + 'Serializer'];
if (typeof serializer === 'function') {
data[prop] = serializer.apply(this, []);
} else {
data[prop] = this.get(prop);
}
}
}
return data;
},
validate: function () {
var errors = {}, ok = true, validator = null, prop = null;
for (prop in this.props) {
if (this.props.hasOwnProperty(prop)) {
try {
validator = this[prop + 'Validator'];
if (typeof validator === 'function') {
validator.apply(this, []);
}
} catch (e) {
errors[prop] = e.message;
ok = false;
}
}
}
if (!ok) {
throw {
errors: errors
};
}
},
initialize: function () {
var prop = null;
for (prop in this.props) {
if (this.props.hasOwnProperty(prop)) {
this.set(prop, this.props[prop]);
}
}
},
getProps: function () {
return this.props;
}
});
});