Permalink
Browse files

Merge branch 'master' of git://github.com/PaulUithol/Backbone-relational

  • Loading branch information...
2 parents f0ea286 + 72cda09 commit a07e7c82688d17d4457c815642db12f869d6f5dc @ulmus ulmus committed Apr 3, 2012
Showing with 62 additions and 14 deletions.
  1. +18 −6 backbone-relational.js
  2. +44 −8 test/tests.js
View
@@ -7,6 +7,8 @@
* Depends on (as in, compeletely useless without) Backbone: https://github.com/documentcloud/backbone.
*/
( function( undefined ) {
+ "use strict";
+
/**
* CommonJS shim
**/
@@ -694,7 +696,7 @@
console.warn( 'Relation=%o; collectionKey=%s already exists on collection=%o', this, key, this.options.collectionKey );
}
}
- else {
+ else if (key) {
collection[ key ] = this.instance;
}
}
@@ -757,10 +759,20 @@
this.related = attr;
}
// Otherwise, 'attr' should be an array of related object ids.
- // Re-use the current 'this.related' if it is a Backbone.Collection.
+ // Re-use the current 'this.related' if it is a Backbone.Collection, and remove any current entries.
+ // Otherwise, create a new collection.
else {
- var coll = this.related instanceof Backbone.Collection ? this.related : new this.collectionType();
- this.setRelated( this.prepareCollection( coll ) );
+ var coll;
+
+ if ( this.related instanceof Backbone.Collection ) {
+ coll = this.related;
+ coll.reset( [], { silent: true } );
+ }
+ else {
+ coll = this.prepareCollection( new this.collectionType() );
+ }
+
+ this.setRelated( coll );
this.findRelated( options );
}
@@ -1152,9 +1164,9 @@
* and 'previousAttributes' will be available when the event is fired.
*/
change: function( options ) {
- var dit = this;
+ var dit = this, args = arguments;
Backbone.Relational.eventQueue.add( function() {
- Backbone.Model.prototype.change.apply( dit, arguments );
+ Backbone.Model.prototype.change.apply( dit, args );
});
},
View
@@ -38,10 +38,15 @@ $(document).ready(function() {
return url;
};
-
-
+
+
+ /**
+ * 'Zoo'
+ */
+
window.Zoo = Backbone.RelationalModel.extend({
- relations: [{
+ relations: [
+ {
type: Backbone.HasMany,
key: 'animals',
relatedModel: 'Animal',
@@ -51,7 +56,13 @@ $(document).ready(function() {
key: 'livesIn',
includeInJSON: 'id'
}
- }]
+ },
+ { // A simple HasMany without recursive relation
+ type: Backbone.HasMany,
+ key: 'visitors',
+ relatedModel: 'Visitor'
+ }
+ ]
});
window.Animal = Backbone.RelationalModel.extend({
@@ -74,6 +85,12 @@ $(document).ready(function() {
}
});
+ window.Visitor = Backbone.RelationalModel.extend();
+
+
+ /**
+ * House/Person/Job/Company
+ */
window.House = Backbone.RelationalModel.extend({
relations: [{
@@ -92,7 +109,8 @@ $(document).ready(function() {
});
window.Person = Backbone.RelationalModel.extend({
- relations: [{
+ relations: [
+ {
// Create a cozy, recursive, one-to-one relationship
type: Backbone.HasOne,
key: 'likesALot',
@@ -157,6 +175,7 @@ $(document).ready(function() {
});
+
window.Node = Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasOne,
@@ -1125,7 +1144,7 @@ $(document).ready(function() {
});
// Add job1 and job2 to the 'Person' side of the relation
- var jobs = person1.get('jobs');
+ var jobs = person1.get( 'jobs' );
jobs.add( job1 );
ok( jobs.length === 1, "jobs.length is 1" );
@@ -1180,6 +1199,23 @@ $(document).ready(function() {
ok( ourHouse.get( 'occupants' ).id === undefined );
});
+
+ test( "Setting a new collection or array of ids updates the relation", function() {
+ var zoo = new Zoo();
+
+ var visitors = [
+ { name: 'Paul' }
+ ];
+
+ zoo.set( 'visitors', visitors );
+
+ equal( zoo.get( 'visitors' ).length, 1 );
+
+ zoo.set( 'visitors', [] );
+
+ equal( zoo.get( 'visitors' ).length, 0 );
+ });
+
test( "Setting a custom collection in 'collectionType' uses that collection for instantiation", function() {
var zoo = new Zoo();
@@ -1199,7 +1235,7 @@ $(document).ready(function() {
ok( zoo.get( 'animals' ) instanceof AnimalCollection );
});
- test( "Settings a new collection maintains that collection's current 'models'", function() {
+ test( "Setting a new collection maintains that collection's current 'models'", function() {
var zoo = new Zoo();
var animals = new AnimalCollection([
@@ -1222,7 +1258,7 @@ $(document).ready(function() {
equal( zoo.get( 'animals' ).length, 3 );
});
- test( "Models found in 'findRelated' are all added in one go (and 'sort' will only be called once)", function() {
+ test( "Models found in 'findRelated' are all added in one go (so 'sort' will only be called once)", function() {
var count = 0,
sort = Backbone.Collection.prototype.sort;

0 comments on commit a07e7c8

Please sign in to comment.