-
Notifications
You must be signed in to change notification settings - Fork 83
/
backbone-nested.js
386 lines (317 loc) · 11.5 KB
/
backbone-nested.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* Backbone-Nested 2.0.4 - An extension of Backbone.js that keeps track of nested attributes
*
* http://afeld.github.com/backbone-nested/
*
* Copyright (c) 2011-2012 Aidan Feldman
* MIT Licensed (LICENSE)
*/
/*global define, require, module */
(function(root, factory){
if (typeof exports !== 'undefined') {
// Define as CommonJS export:
module.exports = factory(require("underscore"), require("backbone"));
} else if (typeof define === 'function' && define.amd) {
// Define as AMD:
define(["underscore", "backbone"], factory);
} else {
// Just run it:
factory(root._, root.Backbone);
}
}(this, function(_, Backbone) {
'use strict';
Backbone.NestedModel = Backbone.Model.extend({
get: function(attrStrOrPath){
return Backbone.NestedModel.walkThenGet(this.attributes, attrStrOrPath);
},
previous: function(attrStrOrPath){
return Backbone.NestedModel.walkThenGet(this._previousAttributes, attrStrOrPath);
},
has: function(attr){
// for some reason this is not how Backbone.Model is implemented - it accesses the attributes object directly
var result = this.get(attr);
return !(result === null || _.isUndefined(result));
},
set: function(key, value, opts){
var newAttrs = Backbone.NestedModel.deepClone(this.attributes),
attrPath,
unsetObj,
validated;
if (_.isString(key)){
// Backbone 0.9.0 syntax: `model.set(key, val)` - convert the key to an attribute path
attrPath = Backbone.NestedModel.attrPath(key);
} else if (_.isArray(key)){
// attribute path
attrPath = key;
}
if (attrPath){
opts = opts || {};
this._setAttr(newAttrs, attrPath, value, opts);
} else { // it's an Object
opts = value || {};
var attrs = key;
for (var _attrStr in attrs) {
if (attrs.hasOwnProperty(_attrStr)) {
this._setAttr(newAttrs,
Backbone.NestedModel.attrPath(_attrStr),
opts.unset ? void 0 : attrs[_attrStr],
opts);
}
}
}
this._nestedChanges = Backbone.NestedModel.__super__.changedAttributes.call(this);
if (opts.unset && attrPath && attrPath.length === 1){ // assume it is a singular attribute being unset
// unsetting top-level attribute
unsetObj = {};
unsetObj[key] = void 0;
this._nestedChanges = _.omit(this._nestedChanges, _.keys(unsetObj));
validated = Backbone.NestedModel.__super__.set.call(this, unsetObj, opts);
} else {
unsetObj = newAttrs;
// normal set(), or an unset of nested attribute
if (opts.unset && attrPath){
// make sure Backbone.Model won't unset the top-level attribute
opts = _.extend({}, opts);
delete opts.unset;
} else if (opts.unset && _.isObject(key)) {
unsetObj = key;
}
this._nestedChanges = _.omit(this._nestedChanges, _.keys(unsetObj));
validated = Backbone.NestedModel.__super__.set.call(this, unsetObj, opts);
}
if (!validated){
// reset changed attributes
this.changed = {};
this._nestedChanges = {};
return false;
}
this._runDelayedTriggers();
return this;
},
unset: function(attr, options) {
return this.set(attr, void 0, _.extend({}, options, {unset: true}));
},
clear: function(options) {
this._nestedChanges = {};
// Mostly taken from Backbone.Model.set, modified to work for NestedModel.
options = options || {};
// clone attributes so validate method can't mutate it from underneath us.
var attrs = _.clone(this.attributes);
if (!options.silent && this.validate && !this.validate(attrs, options)) {
return false; // Should maybe return this instead?
}
var changed = this.changed = {};
var model = this;
var setChanged = function(obj, prefix, options) {
// obj will be an Array or an Object
_.each(obj, function(val, attr){
var changedPath = prefix;
if (_.isArray(obj)){
// assume there is a prefix
changedPath = '[' attr ']';
} else if (prefix){
changedPath = '.' attr;
} else {
changedPath = attr;
}
val = obj[attr];
if (_.isObject(val)) { // clear child attrs
setChanged(val, changedPath, options);
}
if (!options.silent) model._delayedChange(changedPath, null, options);
changed[changedPath] = null;
});
};
setChanged(this.attributes, '', options);
this.attributes = {};
// Fire the `"change"` events.
if (!options.silent) this._delayedTrigger('change');
this._runDelayedTriggers();
return this;
},
add: function(attrStr, value, opts){
var current = this.get(attrStr);
if (!_.isArray(current)) throw new Error('current value is not an array');
return this.set(attrStr '[' current.length ']', value, opts);
},
remove: function(attrStr, opts){
opts = opts || {};
var attrPath = Backbone.NestedModel.attrPath(attrStr),
aryPath = _.initial(attrPath),
val = this.get(aryPath),
i = _.last(attrPath);
if (!_.isArray(val)){
throw new Error("remove() must be called on a nested array");
}
// only trigger if an element is actually being removed
var trigger = !opts.silent && (val.length >= i 1),
oldEl = val[i];
// remove the element from the array
val.splice(i, 1);
opts.silent = true; // Triggers should only be fired in trigger section below
this.set(aryPath, val, opts);
if (trigger){
attrStr = Backbone.NestedModel.createAttrStr(aryPath);
this.trigger('remove:' attrStr, this, oldEl);
for (var aryCount = aryPath.length; aryCount >= 1; aryCount--) {
attrStr = Backbone.NestedModel.createAttrStr(_.first(aryPath, aryCount));
this.trigger('change:' attrStr, this, oldEl);
}
this.trigger('change', this, oldEl);
}
return this;
},
changedAttributes: function(diff) {
var backboneChanged = Backbone.NestedModel.__super__.changedAttributes.call(this, diff);
if (_.isObject(backboneChanged)) {
return _.extend({}, this._nestedChanges, backboneChanged);
}
return false;
},
toJSON: function(){
return Backbone.NestedModel.deepClone(this.attributes);
},
// private
_getDelayedTriggers: function(){
if (typeof this._delayedTriggers === "undefined"){
this._delayedTriggers = [];
}
return this._delayedTriggers;
},
_delayedTrigger: function(/* the trigger args */){
this._getDelayedTriggers().push(arguments);
},
_delayedChange: function(attrStr, newVal, options){
this._delayedTrigger('change:' attrStr, this, newVal, options);
// Check if `change` even *exists*, as it won't when the model is
// freshly created.
if (!this.changed) {
this.changed = {};
}
this.changed[attrStr] = newVal;
},
_runDelayedTriggers: function(){
while (this._getDelayedTriggers().length > 0){
this.trigger.apply(this, this._getDelayedTriggers().shift());
}
},
// note: modifies `newAttrs`
_setAttr: function(newAttrs, attrPath, newValue, opts){
opts = opts || {};
var fullPathLength = attrPath.length;
var model = this;
Backbone.NestedModel.walkPath(newAttrs, attrPath, function(val, path, next){
var attr = _.last(path);
var attrStr = Backbone.NestedModel.createAttrStr(path);
// See if this is a new value being set
var isNewValue = !_.isEqual(val[attr], newValue);
if (path.length === fullPathLength){
// reached the attribute to be set
if (opts.unset){
// unset the value
delete val[attr];
// Trigger Remove Event if array being set to null
if (_.isArray(val)){
var parentPath = Backbone.NestedModel.createAttrStr(_.initial(attrPath));
model._delayedTrigger('remove:' parentPath, model, val[attr]);
}
} else {
// Set the new value
val[attr] = newValue;
}
// Trigger Change Event if new values are being set
if (!opts.silent && _.isObject(newValue) && isNewValue){
var visited = [];
var checkChanges = function(obj, prefix) {
// Don't choke on circular references
if(_.indexOf(visited, obj) > -1) {
return;
} else {
visited.push(obj);
}
var nestedAttr, nestedVal;
for (var a in obj){
if (obj.hasOwnProperty(a)) {
nestedAttr = prefix '.' a;
nestedVal = obj[a];
if (!_.isEqual(model.get(nestedAttr), nestedVal)) {
model._delayedChange(nestedAttr, nestedVal, opts);
}
if (_.isObject(nestedVal)) {
checkChanges(nestedVal, nestedAttr);
}
}
}
};
checkChanges(newValue, attrStr);
}
} else if (!val[attr]){
if (_.isNumber(next)){
val[attr] = [];
} else {
val[attr] = {};
}
}
if (!opts.silent){
// let the superclass handle change events for top-level attributes
if (path.length > 1 && isNewValue){
model._delayedChange(attrStr, val[attr], opts);
}
if (_.isArray(val[attr])){
model._delayedTrigger('add:' attrStr, model, val[attr]);
}
}
});
}
}, {
// class methods
attrPath: function(attrStrOrPath){
var path;
if (_.isString(attrStrOrPath)){
// TODO this parsing can probably be more efficient
path = (attrStrOrPath === '') ? [''] : attrStrOrPath.match(/[^\.\[\]] /g);
path = _.map(path, function(val){
// convert array accessors to numbers
return val.match(/^\d $/) ? parseInt(val, 10) : val;
});
} else {
path = attrStrOrPath;
}
return path;
},
createAttrStr: function(attrPath){
var attrStr = attrPath[0];
_.each(_.rest(attrPath), function(attr){
attrStr = _.isNumber(attr) ? ('[' attr ']') : ('.' attr);
});
return attrStr;
},
deepClone: function(obj){
return JSON.parse(JSON.stringify(obj));
},
walkPath: function(obj, attrPath, callback, scope){
var val = obj,
childAttr;
// walk through the child attributes
for (var i = 0; i < attrPath.length; i ){
callback.call(scope || this, val, attrPath.slice(0, i 1), attrPath[i 1]);
childAttr = attrPath[i];
val = val[childAttr];
if (!val) break; // at the leaf
}
},
walkThenGet: function(attributes, attrStrOrPath){
var attrPath = Backbone.NestedModel.attrPath(attrStrOrPath),
result;
Backbone.NestedModel.walkPath(attributes, attrPath, function(val, path){
var attr = _.last(path);
if (path.length === attrPath.length){
// attribute found
result = val[attr];
}
});
return result;
}
});
return Backbone;
}));