backbone-associations-rails 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
|
|
1
1
|
//
|
2
|
-
// Backbone-associations.js 0.
|
2
|
+
// Backbone-associations.js 0.2.0
|
3
3
|
//
|
4
4
|
// (c) 2012 Dhruva Ray, Jaynti Kanani
|
5
5
|
//
|
@@ -106,7 +106,7 @@
|
|
106
106
|
}
|
107
107
|
//Add proxy events to respective parents
|
108
108
|
this.attributes[relation.key].off("all");
|
109
|
-
this.attributes[relation.key].on("all",function(
|
109
|
+
this.attributes[relation.key].on("all",function(){return this.trigger.apply(this,arguments);},this);
|
110
110
|
//If reference has changed, trigger `change:attribute` event
|
111
111
|
refChanged && this.trigger('change:'+relation.key,options);
|
112
112
|
//Create a local `processedRelations` array to store the relation key which has been processed.
|
@@ -146,44 +146,69 @@
|
|
146
146
|
throw new Error( 'type must inherit from Backbone.AssociatedModel' );
|
147
147
|
}
|
148
148
|
return collection;
|
149
|
-
},
|
149
|
+
},
|
150
|
+
// `trigger` the event for `Associated Model`
|
151
|
+
trigger : function(){
|
152
|
+
//Check & Add `visited` tag to prevent event of cycle
|
153
|
+
if(!this.visited){
|
154
|
+
// mark as `visited`
|
155
|
+
this.visited = true;
|
156
|
+
Backbone.Model.prototype.trigger.apply(this,arguments);
|
157
|
+
//delete `visited` tag to allow trigger for next `set` operation
|
158
|
+
delete this.visited;
|
159
|
+
}
|
160
|
+
return this;
|
161
|
+
},
|
150
162
|
//The JSON representation of the model.
|
151
|
-
toJSON : function(){
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
163
|
+
toJSON : function(){
|
164
|
+
var json;
|
165
|
+
if(!this.visited){
|
166
|
+
this.visited = true;
|
167
|
+
//Get json representation from `Backbone.Model.prototype.toJSON`
|
168
|
+
json = Backbone.Model.prototype.toJSON.apply( this, arguments );
|
169
|
+
//If `this.relations` is defined, iterate through each `relation` and added it's json representation to parents' json representation
|
170
|
+
if(this.relations){
|
171
|
+
_.each(this.relations ,function(relation){
|
172
|
+
var attr = this.attributes[relation.key];
|
173
|
+
if(attr){
|
174
|
+
aJson = attr.toJSON();
|
175
|
+
json[relation.key] = _.isArray(aJson)?_.compact(aJson):aJson;
|
176
|
+
}
|
177
|
+
},this);
|
178
|
+
}
|
179
|
+
delete this.visited;
|
161
180
|
}
|
162
181
|
return json;
|
163
182
|
},
|
164
183
|
//deep `clone` the model.
|
165
184
|
clone : function(){
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
var
|
176
|
-
|
177
|
-
|
178
|
-
newCollection.
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
185
|
+
var cloneObj;
|
186
|
+
if(!this.visited){
|
187
|
+
this.visited = true;
|
188
|
+
//Get shallow clone from `Backbone.Model.prototype.clone`
|
189
|
+
cloneObj = Backbone.Model.prototype.clone.apply( this, arguments );
|
190
|
+
//If `this.relations` is defined, iterate through each `relation` and `clone`
|
191
|
+
if(this.relations){
|
192
|
+
_.each(this.relations ,function(relation){
|
193
|
+
if(this.attributes[relation.key]){
|
194
|
+
var sourceObj = cloneObj.attributes[relation.key];
|
195
|
+
if(sourceObj instanceof Backbone.Collection){
|
196
|
+
//Creates new `collection` using `relation`
|
197
|
+
var newCollection = relation.collectionType ? new relation.collectionType() : this._createCollection(relation.relatedModel);
|
198
|
+
//Added each `clone` model to `newCollection`
|
199
|
+
sourceObj.each(function(model){
|
200
|
+
var mClone = model.clone()
|
201
|
+
mClone && newCollection.add(mClone);
|
202
|
+
});
|
203
|
+
cloneObj.attributes[relation.key] = newCollection;
|
204
|
+
}
|
205
|
+
else if(sourceObj instanceof Backbone.Model){
|
206
|
+
cloneObj.attributes[relation.key] = sourceObj.clone();
|
207
|
+
}
|
184
208
|
}
|
185
|
-
}
|
186
|
-
}
|
209
|
+
},this);
|
210
|
+
}
|
211
|
+
delete this.visited;
|
187
212
|
}
|
188
213
|
return cloneObj;
|
189
214
|
}
|