-
Notifications
You must be signed in to change notification settings - Fork 0
/
ractive-object-observe.js
83 lines (70 loc) · 2.39 KB
/
ractive-object-observe.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
(function() {
if(typeof Object.observe === 'undefined') {
console.error("Object.observe not supported (or enabled) in your browser! If you're using Chrome, go to chrome://flags and 'Enable Experimental JavaScript'");
}
var ObjectObserveWrapper = function(ractive, obj, keypath, prefix) {
this.value = obj;
this.keypath = keypath;
this.observeCallback = function(changes) {
var keyPathsToUpdate = [];
function scheduleKeyPathUpdate(keyPath) {
if(keyPathsToUpdate.indexOf(keyPath) === -1) {
keyPathsToUpdate.push(keyPath);
}
}
changes.forEach(function(change) {
switch (change.type) {
case "updated":
scheduleKeyPathUpdate(Object.keys(prefix(change.name))[0]);
break;
case "new":
case "splice":
case "deleted":
var keyPath = Object.keys(prefix(change.name))[0];
// Array deletions (task.list.length = 0) will trigger one change pr. element
// in order to update ractive only once, we normalize the keyPath to represent their containing
// array instead, e.g.:
// task.list.1 => task.list
// This allows us to trigger 1 update instead of N updates
keyPath = keyPath.replace(/\.[0-9]+$/, "");
scheduleKeyPathUpdate(keyPath);
break;
default:
console.error("Unhandled change event: " + change.type);
break;
}
});
keyPathsToUpdate.forEach(function(kp) {
ractive.update(kp.keyPath);
});
};
Object.observe(this.value, this.observeCallback);
};
ObjectObserveWrapper.prototype = {
teardown: function() {
Object.unobserve(this.value, this.observeCallback);
},
get: function() {
return this.value;
},
set: function(keypath, value) {
this.value[keypath] = value;
},
reset: function() {
this.value = {};
}
};
Ractive.adaptors.ObjectObserve = {
filter: function(object) {
return typeof object === "object";
},
wrap: function(ractive, object, keypath, prefix) {
if(ractive.modifyArrays) {
console.error("Object.Observe incompatible with modifyArrays: true. Set to false and try again!");
return null;
} else {
return new ObjectObserveWrapper(ractive, object, keypath, prefix);
}
}
};
}());