-
Notifications
You must be signed in to change notification settings - Fork 2
/
rivets-backbone.js
148 lines (129 loc) · 3.83 KB
/
rivets-backbone.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* global define: true */
(function (root, factory) {
'use strict';
if (typeof exports === 'object') {
// CommonJS
factory(require('rivets'), require('Backbone'));
} else if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['rivets', 'Backbone'], factory);
} else {
// Browser globals
factory(root.rivets, root.Backbone);
}
})
(this, function (rivets, Backbone) {
'use strict';
var Model = Backbone.Model,
Collection = Backbone.Collection;
/**
* Resolves path chain
*
* for a, 'b.c.d' returns {model: a.b.c, key:'d'}
*
* @param {Model} model
* @param {String} keypath
*
* @returns {{model: Model, key: String}}
*/
function getKeyPathRoot(model, keypath, limit) {
var paths
;
keypath = keypath.split('.');
paths = keypath.length;
for(var i = 1; keypath.length > 1 && (typeof limit === 'undefined' || i < limit); i++) {
model = model.get(keypath.shift());
}
return {
model: model,
key: keypath.shift(),
paths: paths
};
}
/**
* @param {Model} model
* @param {String} keypath
* @param {*} [value]
*
* @returns {*}
*/
function getterSetter(model, keypath, value) {
var root = getKeyPathRoot(model, keypath);
model = root.model;
if (arguments.length === 2) {
return model.get(root.key);
}
model.set(root.key, value);
}
/**
* @param {String} action on or off
* @returns {Function}
*/
function onOffFactory(action) {
/**
* @param {Model} model
* @param {String} keypath
* @param {Function} callback
*/
return function (model, keypath, callback) {
if (!(model instanceof Model)) {
return;
}
var root = getKeyPathRoot(model, keypath)
, collection = root.model.get(root.key)
, stepWiseRoot
;
if (collection instanceof Collection) {
collection[action]('add remove reset', callback);
} else {
root.model[action]('change:' + root.key, callback);
// Ensure each nested level gets triggered as well (replacing eventual bubbling)
if(root.model.cid !== model.cid) {
for(var i = 1; i < root.paths; i++) {
stepWiseRoot = getKeyPathRoot(model, keypath, i);
stepWiseRoot.model[action]('change:' + stepWiseRoot.key, callback);
}
}
}
};
}
/**
* @param {Model|Collection} obj
* @param {String} keypath
* @returns {*}
*/
function read(obj, keypath) {
if (obj instanceof Collection) {
return obj[keypath];
}
var value = getterSetter(obj, keypath);
// rivets cant iterate over Backbone.Collection -> return Array
if (value instanceof Collection) {
return value.models;
}
return value;
}
/**
* @param {Model|Collection} obj
* @param {String} keypath
* @param {*} value
*/
function publish(obj, keypath, value) {
if (obj instanceof Collection) {
obj[keypath] = value;
} else {
getterSetter(obj, keypath, value);
}
}
// Configure rivets data-bind for Backbone.js
rivets.configure({
prefix: 'rv'
, preloadData: false
, adapter: {
subscribe: onOffFactory('on'),
unsubscribe: onOffFactory('off'),
read: read,
publish: publish
}
});
});