-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.js
81 lines (69 loc) · 1.35 KB
/
Model.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
class Model {
constructor(datasource) {
this.store = datasource;
}
loadData(datasource) {
this.store = datasource;
this.emit('change');
}
fill(data) {
if (_.isObject(data)) {
for (let key in data) {
if (data.hasOwnProperty(key)) {
this.store[key] = data[key];
this.emit('change', {
key,
value: data[key]
});
}
}
console.log(this);
} else {
_.handleError('fill data must be Object');
}
}
remove(name) {
if (this.has(name)) {
let value = this.store[name];
delete this.store[name];
this.emit('delete', {
name,
value: value
});
return value;
} else {
return;
}
}
has(name) {
return this.store.hasOwnProperty(name);
}
get(name) {
return this.store[name];
}
set(name, value) {
this.store[name] = value;
this.emit('change', {
name,
value: value
});
return value;
}
fetch(options) {
return ajax(options)
.then(data => {
this.fill({
result: JSON.parse(data)
});
return data;
})
.catch(err => console.log(err));
}
hasValue(name) {
return this.has(name) && this.store[name] !== null;
}
// 导出model为普通对象
export() {
return _.clone(this.store);
}
}