-
Notifications
You must be signed in to change notification settings - Fork 28
/
backbone-nested-models.js
194 lines (159 loc) · 5.91 KB
/
backbone-nested-models.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
/**
* Backbone Nested Models
* Author: Bret Little
* Version: 2.0.4
*
* Nested model support in Backbone.js
*
**/
// support amd and common js
(function (root, factory) {
if (typeof exports === 'object') {
// CommonJS
module.exports = factory(require('backbone'), require('underscore'));
} else if (typeof define === 'function' && define.amd) {
// AMD
define(['backbone', 'underscore'], function (b, u) {
return (root.returnExportsGlobal = factory(b, u));
});
} else {
// Global Variables
root.returnExportsGlobal = factory(root.Backbone, root._);
}
}(this, function (Backbone, _) {
var Model = Backbone.Model,
Collection = Backbone.Collection;
Backbone.Model.prototype.setRelation = function(attr, val, options) {
var relation = this.attributes[attr],
id = this.idAttribute || "id",
modelToSet, modelsToAdd = [], modelsToRemove = [];
if(options.unset && relation) delete relation.parent;
if(this.relations && _.has(this.relations, attr)) {
// If the relation already exists, we don't want to replace it, rather
// update the data within it whether it is a collection or model
if(relation && relation instanceof Collection) {
// `val` can come in as a Collection, Array, or single Model.
// Ensure that it is an Array for use with `collection.set()`.
if(val instanceof Collection || val instanceof Array) {
val = val.models || val;
} else {
val = [val];
}
relation.set(val, options);
return relation;
}
if(relation && relation instanceof Model) {
// `val` can come in as a Model or attributes hash.
// Ensure that it is an attributes hash for use with `model.set()`.
if (val instanceof Model) {
val = val.toJSON()
}
if (options.parse) val = relation.parse(val, options);
relation.set(val, options);
return relation;
}
options._parent = this;
if (val instanceof Collection || val instanceof Model) {
val = val.toJSON();
}
val = new this.relations[attr](val, options);
val.parent = this;
}
return val;
};
Backbone.Model.prototype.set = function(key, val, options) {
var attr, attrs, unset, changes, silent, changing, prev, current;
if (key == null) return this;
// Handle both `"key", value` and `{key: value}` -style arguments.
if (typeof key === 'object') {
attrs = key;
options = val;
} else {
(attrs = {})[key] = val;
}
options || (options = {});
// Run validation.
if (!this._validate(attrs, options)) return false;
// Extract attributes and options.
unset = options.unset;
silent = options.silent;
changes = [];
changing = this._changing;
this._changing = true;
if (!changing) {
this._previousAttributes = _.clone(this.attributes);
this.changed = {};
}
current = this.attributes, prev = this._previousAttributes;
// Check for changes of `id`.
if (this.idAttribute in attrs) this.id = attrs[this.idAttribute];
// For each `set` attribute, update or delete the current value.
for (attr in attrs) {
val = attrs[attr];
// Inject in the relational lookup
val = this.setRelation(attr, val, options);
if (!_.isEqual(current[attr], val)) changes.push(attr);
if (!_.isEqual(prev[attr], val)) {
this.changed[attr] = val;
} else {
delete this.changed[attr];
}
unset ? delete current[attr] : current[attr] = val;
}
// Trigger all relevant attribute changes.
if (!silent) {
if (changes.length) this._pending = true;
for (var i = 0, l = changes.length; i < l; i++) {
this.trigger('change:' + changes[i], this, current[changes[i]], options);
}
}
if (changing) return this;
if (!silent) {
while (this._pending) {
this._pending = false;
this.trigger('change', this, options);
}
}
this._pending = false;
this._changing = false;
return this;
};
Backbone.Model.prototype.toJSON = function(options) {
var attrs = _.clone(this.attributes);
_.each(this.relations, function(rel, key) {
if (_.has(attrs, key)) {
attrs[key] = attrs[key].toJSON(options);
} else {
attrs[key] = (new rel()).toJSON(options);
}
});
return attrs;
};
Backbone.Model.prototype.clone = function(options) {
return new this.constructor(this.toJSON());
};
Backbone.Collection.prototype.resetRelations = function(options) {
_.each(this.models, function(model) {
_.each(model.relations, function(rel, key) {
if(model.get(key) instanceof Backbone.Collection) {
model.get(key).trigger('reset', model, options);
}
});
})
};
Backbone.Collection.prototype.reset = function(models, options) {
options || (options = {});
for (var i = 0, l = this.models.length; i < l; i++) {
this._removeReference(this.models[i]);
}
options.previousModels = this.models;
this._reset();
this.add(models, _.extend({silent: true}, options));
if (!options.silent) {
this.trigger('reset', this, options);
this.resetRelations(options);
}
return this;
};
return Backbone;
}));