mock.data.js
731 Bytes
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
function Data(attributes) {
this.attributes = attributes || {};
this.change = {}
}
Data.prototype.on = function(key, callback) {
this.change[key][callback] = true;
}
Data.prototype.off = function(key, callback) {
delete this.change[key][callback];
}
Data.prototype.set = function(attributes) {
var old, key;
for(key in attributes) {
old = this.attributes[key];
this.attributes[key] = attributes[key];
if(this.get(key) !== old)
this.alertCallbacks(key);
}
}
Data.prototype.get = function(key) {
this.attributes[key];
}
Data.prototype.alertCallbacks = function(key) {
if(!this.change[key])
return;
for(callback in this.change[key])
callback(this.get(key));
}
window.Data = Data;