-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreactable.js
43 lines (40 loc) · 1.52 KB
/
reactable.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
Object.prototype.watch = function(prop, fn, originalObj, originalProp) {
if(typeof prop == 'object') {
for(var p in prop) {
if(prop.hasOwnProperty(p)) {
prop.watch.bind(prop)(p, fn, prop, p);
}
}
return prop;
} else {
if(!originalObj) originalObj = this;
if(!originalProp) originalProp = prop;
let curVal = this[prop];
if(typeof curVal == 'object') {
for(var p in curVal) {
if(curVal.hasOwnProperty(p)) curVal.watch(p, fn, originalObj, originalProp);
}
}
fn = fn.bind(originalObj);
let watcher = {};
watcher[prop] = {
get: function() { return curVal; },
set: function(newVal) {
if(curVal == newVal) return;
if(typeof newVal == 'object') {
for(var p in newVal) {
if(newVal.hasOwnProperty(p)) newVal.watch(p, fn, originalObj, originalProp);
}
}
let beforeVal = curVal;
let before = originalObj[originalProp];
try { before = JSON.parse(JSON.stringify(before)); } catch(e) {}
curVal = newVal;
let after = originalObj[originalProp];
try { after = JSON.parse(JSON.stringify(after)); } catch(e) {}
fn(originalProp, before, after, prop, beforeVal, newVal);
}
};
Object.defineProperties(this, watcher);
}
};