Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/PaulUithol/Backbone-relational
Browse files Browse the repository at this point in the history
  • Loading branch information
ulmus committed Apr 3, 2012
2 parents f0ea286 + 72cda09 commit a07e7c8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 14 deletions.
24 changes: 18 additions & 6 deletions backbone-relational.js
Expand Up @@ -7,6 +7,8 @@
* Depends on (as in, compeletely useless without) Backbone: https://github.com/documentcloud/backbone.
*/
( function( undefined ) {
"use strict";

/**
* CommonJS shim
**/
Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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 );
}

Expand Down Expand Up @@ -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 );
});
},

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 @@ -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({
Expand All @@ -74,6 +85,12 @@ $(document).ready(function() {
}
});

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


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

window.House = Backbone.RelationalModel.extend({
relations: [{
Expand All @@ -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',
Expand Down Expand Up @@ -157,6 +175,7 @@ $(document).ready(function() {
});



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

Expand All @@ -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([
Expand All @@ -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;

Expand Down

0 comments on commit a07e7c8

Please sign in to comment.