Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: dhruvaray/backbone-associations
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v0.1.0
Choose a base ref
...
head repository: dhruvaray/backbone-associations
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v0.2.0
Choose a head ref
  • 3 commits
  • 6 files changed
  • 2 contributors

Commits on Aug 25, 2012

  1. Fixed typo

    dhruvaray committed Aug 25, 2012
    Copy the full SHA
    19316a4 View commit details

Commits on Sep 4, 2012

  1. Added support for cyclic object graph.

    -added visited tag for trigger,toJSON,clone method to avoid cycle.
    -on the result of this, each model get triggered only once in close graph for
    given changed attributes.
    -added some test cases to explain cyclic graph and expected result for toJSON
    and clone methods.
    jdkanani committed Sep 4, 2012
    Copy the full SHA
    a4e2d51 View commit details
  2. Copy the full SHA
    805e6b3 View commit details
Showing with 231 additions and 70 deletions.
  1. +9 −7 README.md
  2. +6 −6 backbone-associations-min.js
  3. +58 −33 backbone-associations.js
  4. +39 −22 docs/backbone-associations.html
  5. +2 −2 package.json
  6. +117 −0 test/associated-model.js
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -12,18 +12,18 @@ The [annotated](http://dhruvaray.github.com/backbone-associations/docs/backbone-
* [Installation](#installation)
* [Specifying Associations](#associations)
* [Eventing with Associations](#eventing)
* [Known Issues](#issues)
* [Change Log](#changelog)


### <a name="download"/>Download

* [Minified version 0.1 ](http://dhruvaray.github.com/backbone-associations/backbone-associations-min.js)
* [Development version 0.1](http://dhruvaray.github.com/backbone-associations/backbone-associations.js)
* [Minified version 0.2.0 ](http://dhruvaray.github.com/backbone-associations/backbone-associations-min.js)
* [Development version 0.2.0](http://dhruvaray.github.com/backbone-associations/backbone-associations.js)


### <a name="installation"/>Installation

Backbone-associations depends on [backbone](https://github.com/documentcloud/backbone) (and thus on [underscore](https://github.com/documentcloud/underscore)). Include Backbone-relational right after Backbone and Underscore:
Backbone-associations depends on [backbone](https://github.com/documentcloud/backbone) (and thus on [underscore](https://github.com/documentcloud/underscore)). Include Backbone-associations right after Backbone and Underscore:

```html
<script type="text/javascript" src="./js/underscore.js"></script>
@@ -173,8 +173,10 @@ project1.get('locations').reset();

````

## <a name="issues"/>Known Issues
## <a name="changelog"/>Change Log
#### Version 0.2.0
Added support for cyclic object graphs.

#### Version 0.1
Cyclic object graphs are not supported.
#### Version 0.1.0
Initial Backbone-associations release.

12 changes: 6 additions & 6 deletions backbone-associations-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

91 changes: 58 additions & 33 deletions backbone-associations.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Backbone-associations.js 0.1.0
// Backbone-associations.js 0.2.0
//
// (c) 2012 Dhruva Ray, Jaynti Kanani
//
@@ -106,7 +106,7 @@
}
//Add proxy events to respective parents
this.attributes[relation.key].off("all");
this.attributes[relation.key].on("all",function(events){this.trigger(events,_.rest(arguments))},this);
this.attributes[relation.key].on("all",function(){return this.trigger.apply(this,arguments);},this);
//If reference has changed, trigger `change:attribute` event
refChanged && this.trigger('change:'+relation.key,options);
//Create a local `processedRelations` array to store the relation key which has been processed.
@@ -146,44 +146,69 @@
throw new Error( 'type must inherit from Backbone.AssociatedModel' );
}
return collection;
},
},
// `trigger` the event for `Associated Model`
trigger : function(){
//Check & Add `visited` tag to prevent event of cycle
if(!this.visited){
// mark as `visited`
this.visited = true;
Backbone.Model.prototype.trigger.apply(this,arguments);
//delete `visited` tag to allow trigger for next `set` operation
delete this.visited;
}
return this;
},
//The JSON representation of the model.
toJSON : function(){
//Get json representation from `Backbone.Model.prototype.toJSON`
var json = Backbone.Model.prototype.toJSON.apply( this, arguments );
//If `this.relations` is defined, iterate through each `relation` and added it's json representation to parents' json representation
if(this.relations){
_.each(this.relations ,function(relation){
if(this.attributes[relation.key]){
json[relation.key] = (this.attributes[relation.key].cid !== this.cid) ? this.attributes[relation.key].toJSON() : void 0;
}
},this);
toJSON : function(){
var json;
if(!this.visited){
this.visited = true;
//Get json representation from `Backbone.Model.prototype.toJSON`
json = Backbone.Model.prototype.toJSON.apply( this, arguments );
//If `this.relations` is defined, iterate through each `relation` and added it's json representation to parents' json representation
if(this.relations){
_.each(this.relations ,function(relation){
var attr = this.attributes[relation.key];
if(attr){
aJson = attr.toJSON();
json[relation.key] = _.isArray(aJson)?_.compact(aJson):aJson;
}
},this);
}
delete this.visited;
}
return json;
},
//deep `clone` the model.
clone : function(){
//Get shallow clone from `Backbone.Model.prototype.clone`
var cloneObj = Backbone.Model.prototype.clone.apply( this, arguments );
//If `this.relations` is defined, iterate through each `relation` and `clone`
if(this.relations){
_.each(this.relations ,function(relation){
if(this.attributes[relation.key]){
var sourceObj = cloneObj.attributes[relation.key];
if(sourceObj instanceof Backbone.Collection){
//Creates new `collection` using `relation`
var newCollection = relation.collectionType ? new relation.collectionType() : this._createCollection(relation.relatedModel);
//Added each `clone` model to `newCollection`
sourceObj.each(function(model){
newCollection.add(model.clone());
});
cloneObj.attributes[relation.key] = newCollection;
}
else if(sourceObj instanceof Backbone.Model){
cloneObj.attributes[relation.key] = sourceObj.clone();
var cloneObj;
if(!this.visited){
this.visited = true;
//Get shallow clone from `Backbone.Model.prototype.clone`
cloneObj = Backbone.Model.prototype.clone.apply( this, arguments );
//If `this.relations` is defined, iterate through each `relation` and `clone`
if(this.relations){
_.each(this.relations ,function(relation){
if(this.attributes[relation.key]){
var sourceObj = cloneObj.attributes[relation.key];
if(sourceObj instanceof Backbone.Collection){
//Creates new `collection` using `relation`
var newCollection = relation.collectionType ? new relation.collectionType() : this._createCollection(relation.relatedModel);
//Added each `clone` model to `newCollection`
sourceObj.each(function(model){
var mClone = model.clone()
mClone && newCollection.add(mClone);
});
cloneObj.attributes[relation.key] = newCollection;
}
else if(sourceObj instanceof Backbone.Model){
cloneObj.attributes[relation.key] = sourceObj.clone();
}
}
}
},this);
},this);
}
delete this.visited;
}
return cloneObj;
}
Loading