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,7 +1,7 @@
1
1
  module Backbone
2
2
  module Associations
3
3
  module Rails
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
6
6
  end
7
7
  end
@@ -1,5 +1,5 @@
1
1
  //
2
- // Backbone-associations.js 0.1.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(events){this.trigger(events,_.rest(arguments))},this);
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
- //Get json representation from `Backbone.Model.prototype.toJSON`
153
- var json = Backbone.Model.prototype.toJSON.apply( this, arguments );
154
- //If `this.relations` is defined, iterate through each `relation` and added it's json representation to parents' json representation
155
- if(this.relations){
156
- _.each(this.relations ,function(relation){
157
- if(this.attributes[relation.key]){
158
- json[relation.key] = (this.attributes[relation.key].cid !== this.cid) ? this.attributes[relation.key].toJSON() : void 0;
159
- }
160
- },this);
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
- //Get shallow clone from `Backbone.Model.prototype.clone`
167
- var cloneObj = Backbone.Model.prototype.clone.apply( this, arguments );
168
- //If `this.relations` is defined, iterate through each `relation` and `clone`
169
- if(this.relations){
170
- _.each(this.relations ,function(relation){
171
- if(this.attributes[relation.key]){
172
- var sourceObj = cloneObj.attributes[relation.key];
173
- if(sourceObj instanceof Backbone.Collection){
174
- //Creates new `collection` using `relation`
175
- var newCollection = relation.collectionType ? new relation.collectionType() : this._createCollection(relation.relatedModel);
176
- //Added each `clone` model to `newCollection`
177
- sourceObj.each(function(model){
178
- newCollection.add(model.clone());
179
- });
180
- cloneObj.attributes[relation.key] = newCollection;
181
- }
182
- else if(sourceObj instanceof Backbone.Model){
183
- cloneObj.attributes[relation.key] = sourceObj.clone();
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
- },this);
209
+ },this);
210
+ }
211
+ delete this.visited;
187
212
  }
188
213
  return cloneObj;
189
214
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backbone-associations-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: