Skip to content

Commit

Permalink
Do reset a HasMany's collection when the relation gets 'set' with som…
Browse files Browse the repository at this point in the history
…ething that isn't a Collection. Regression from PaulUithol#90; fixes PaulUithol#100.
  • Loading branch information
PaulUithol committed Apr 2, 2012
1 parent 6479441 commit 72cda09
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 11 deletions.
16 changes: 13 additions & 3 deletions backbone-relational.js
Expand Up @@ -755,10 +755,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 );
}

Expand Down
52 changes: 44 additions & 8 deletions test/tests.js
Expand Up @@ -38,10 +38,15 @@ $(document).ready(function() {

return url;
};




/**
* 'Zoo'
*/

window.Zoo = Backbone.RelationalModel.extend({
relations: [{
relations: [
{
type: Backbone.HasMany,
key: 'animals',
relatedModel: 'Animal',
Expand All @@ -50,7 +55,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({
Expand All @@ -68,6 +79,12 @@ $(document).ready(function() {
model: Animal
});

window.Visitor = Backbone.RelationalModel.extend();


/**
* House/Person/Job/Company
*/

window.House = Backbone.RelationalModel.extend({
relations: [{
Expand All @@ -86,7 +103,8 @@ $(document).ready(function() {
});

window.Person = Backbone.RelationalModel.extend({
relations: [{
relations: [
{
// Create a cozy, recursive, one-to-one relationship
type: Backbone.HasOne,
key: 'likesALot',
Expand Down Expand Up @@ -151,6 +169,7 @@ $(document).ready(function() {
});



window.Node = Backbone.RelationalModel.extend({
relations: [{
type: Backbone.HasOne,
Expand Down Expand Up @@ -1114,7 +1133,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" );
Expand Down Expand Up @@ -1169,6 +1188,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();

Expand All @@ -1188,7 +1224,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([
Expand All @@ -1211,7 +1247,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;

Expand Down

0 comments on commit 72cda09

Please sign in to comment.