-
Notifications
You must be signed in to change notification settings - Fork 0
/
adapter.js
54 lines (47 loc) · 1.27 KB
/
adapter.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
// Rivets Adapter
(function() {
var onOff = function(action) {
return function(o, path, cb) {
if (o && o[action])
o[action](path ? 'change:' + path : 'create remove change', cb);
}
};
var readPublish = function(o, path, value) {
if (!path)
return o && o.get ? o.get() : o;
var p = path.split('.'),
model = o,
read = (arguments.length === 2),
whiles = read ? 0 : 1;
while (p.length > whiles) {
if (o == undefined)
return o;
if (o.get) {
model = o;
path = p.join('.');
o = o.get(p.shift());
}
else
o = o[p.shift()];
}
if (read)
return o;
if (o.set)
value = o.set(p.shift(), value);
else if (o.call)
value = o(value);
else
o[p.shift()] = value;
if (model.trigger)
model.trigger('change:'+path);
return value;
};
rivets.configure({
adapter: {
subscribe: onOff('on'),
unsubscribe: onOff('off'),
read: readPublish,
publish: readPublish
}
});
})();