Permalink
Browse files

Add option to define `relatedModel` or `collectionType` as a function

  • Loading branch information...
PaulUithol committed Aug 16, 2013
1 parent 07d1dc5 commit 7f1dc51a820aa4a5e22303177cffbbc8e7c53d47
Showing with 27 additions and 18 deletions.
  1. +9 −1 backbone-relational.js
  2. +18 −17 test/tests.js
View
@@ -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 );
}
@@ -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 );
}
@@ -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 );
View
@@ -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++;
}
@@ -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++;
}
@@ -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;
});
@@ -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' );
});
@@ -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++;
});

0 comments on commit 7f1dc51

Please sign in to comment.