Skip to content

Commit

Permalink
Add option to define relatedModel or collectionType as a function
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulUithol committed Aug 16, 2013
1 parent 07d1dc5 commit 7f1dc51
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
10 changes: 9 additions & 1 deletion backbone-relational.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,12 @@
this.keyDestination = this.options.keyDestination || this.keySource || this.key;

this.model = this.options.model || this.instance.constructor;

this.relatedModel = this.options.relatedModel;

if ( _.isFunction( this.relatedModel ) && !( this.relatedModel.prototype instanceof Backbone.RelationalModel ) ) {
this.relatedModel = _.result( this, 'relatedModel' );
}
if ( _.isString( this.relatedModel ) ) {
this.relatedModel = Backbone.Relational.store.getObjectByName( this.relatedModel );
}
Expand Down Expand Up @@ -845,6 +850,9 @@

// Handle a custom 'collectionType'
this.collectionType = this.options.collectionType;
if ( _.isFunction( this.collectionType ) && this.collectionType !== Backbone.Collection && !( this.collectionType.prototype instanceof Backbone.Collection ) ) {
this.collectionType = _.result( this, 'collectionType' );
}
if ( _.isString( this.collectionType ) ) {
this.collectionType = Backbone.Relational.store.getObjectByName( this.collectionType );
}
Expand Down Expand Up @@ -1194,7 +1202,7 @@
this.acquire(); // Setting up relations often also involve calls to 'set', and we only want to enter this function once
this._relations = {};

_.each( _.result(this, 'relations') || [], function( rel ) {
_.each( _.result( this, 'relations' ) || [], function( rel ) {
Backbone.Relational.store.initializeRelation( this, rel, options );
}, this );

Expand Down
35 changes: 18 additions & 17 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ $(document).ready(function() {
var Animal2 = Animal.extend({
initialize: function(options) {
this.on( 'all', function( name, event ) {
console.log( 'Animal2: %o', arguments );
//console.log( 'Animal2: %o', arguments );
if ( name.indexOf( 'change' ) === 0 ) {
modelChangeEvents++;
}
Expand All @@ -1367,7 +1367,7 @@ $(document).ready(function() {

initialize: function(options) {
this.on( 'all', function( name, event ) {
console.log( 'AnimalCollection2: %o', arguments );
//console.log( 'AnimalCollection2: %o', arguments );
if ( name.indexOf('change') === 0 ) {
collectionChangeEvents++;
}
Expand Down Expand Up @@ -2720,34 +2720,36 @@ $(document).ready(function() {
{
type: Backbone.HasMany,
key: 'animals',
relatedModel: 'Animal',
relatedModel: function() {
return Animal; // or `require` it from somewhere
},
includeInJSON: [ 'id', 'species' ],
collectionType: 'AnimalCollection',
collectionType: function() {
return AnimalCollection; // or `require` it from somewhere
},
collectionOptions: function( instance ) { return { 'url': 'zoo/' + instance.cid + '/animal/' } },
reverseRelation: {
key: 'livesIn',
includeInJSON: [ 'id', 'name' ]
}
},
{ // A simple HasMany without reverse relation
type: Backbone.HasMany,
key: 'visitors',
relatedModel: 'Visitor'
}
];
};

var animals = new AnimalCollection([
var animalData = [
{ id: 1, species: 'Lion' },
{ id: 2 ,species: 'Zebra' }
]);
];

var zoo = new Zoo( { animals: animals } );
var zoo = new Zoo( { animals: animalData } ),
animals = zoo.get( 'animals' );

equal( zoo.get( 'animals' ), animals, "The 'animals' collection has been set as the zoo's animals" );
equal( zoo.get( 'animals' ).length, 2, "Two animals in 'zoo'" );
ok( animals instanceof AnimalCollection );
equal( animals.length, 2, "Two animals in 'zoo'" );
ok( animals.at( 0 ) instanceof Animal );

zoo.destroy();

window.Zoo.prototype.relations = window.Zoo.prototype._relations;
delete window.Zoo.prototype._relations;
});
Expand Down Expand Up @@ -4037,7 +4039,6 @@ $(document).ready(function() {
var a = animals.pop(),
b = animals.pop();

console.log( a, a.get( 'name' ), b );
ok( a && a.get( 'name' ) === 'a' );
ok( typeof b === 'undefined' );
});
Expand Down Expand Up @@ -4340,11 +4341,11 @@ $(document).ready(function() {

person
.on('change:livesIn', function() {
console.log( arguments );
//console.log( arguments );
house.set({livesIn: house});
})
.on( 'change', function () {
console.log( arguments );
//console.log( arguments );
changeEventsTriggered++;
});

Expand Down

0 comments on commit 7f1dc51

Please sign in to comment.