Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #360. Adds support for deep subModelType hierarchies. #362

Merged
merged 3 commits into from
Jul 23, 2013
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fixes #360. Adds support for deep subModelType hierarchies.
Recursively resolves the appropriate subModelType to instantiate in
'Backbone.RelationalModel.build'. This allows subModelTypes to in turn declare
their own subModelTypes.
Gordon McNaughton committed Jun 13, 2013
commit 2b0de9933a9da173778c6eadaefeeff2e21c4800
37 changes: 28 additions & 9 deletions backbone-relational.js
Original file line number Diff line number Diff line change
@@ -1569,22 +1569,41 @@
* @return {Backbone.Model}
*/
build: function( attributes, options ) {
var model = this;

// 'build' is a possible entrypoint; it's possible no model hierarchy has been determined yet.
this.initializeModelHierarchy();

// Determine what type of (sub)model should be built if applicable.
// Lookup the proper subModelType in 'this._subModels'.
if ( this._subModels && this.prototype.subModelTypeAttribute in attributes ) {
var subModelTypeAttribute = attributes[ this.prototype.subModelTypeAttribute ];
var subModelType = this._subModels[ subModelTypeAttribute ];
var model = this._findSubModelType(this, attributes) || this;

return new model( attributes, options );
},

/**
* Determines what type of (sub)model should be built if applicable.
* Looks up the proper subModelType in 'this._subModels', recursing into
* types until a match is found. Returns the applicable 'Backbone.Model'
* or null if no match is found.
* @param {Backbone.Model} type
* @param {Object} attributes
* @return {Backbone.Model}
*/
_findSubModelType: function (type, attributes) {
if ( type._subModels && type.prototype.subModelTypeAttribute in attributes ) {
var subModelTypeAttribute = attributes[type.prototype.subModelTypeAttribute];
var subModelType = type._subModels[subModelTypeAttribute];
if ( subModelType ) {
model = subModelType;
return subModelType;
} else {
// Recurse into subModelTypes to find a match
for ( subModelTypeAttribute in type._subModels ) {
subModelType = this._findSubModelType(type._subModels[subModelTypeAttribute], attributes);
if ( subModelType ) {
return subModelType;
}
}
}
}

return new model( attributes, options );
return null;
},

/**
52 changes: 44 additions & 8 deletions test/tests.js
Original file line number Diff line number Diff line change
@@ -1396,7 +1396,12 @@ $(document).ready(function() {
'carnivore': 'Carnivore'
}
});
scope.Primate = scope.Mammal.extend();
scope.Primate = scope.Mammal.extend({
subModelTypes: {
'human': 'Human'
}
});
scope.Human = scope.Primate.extend();
scope.Carnivore = scope.Mammal.extend();

var MammalCollection = AnimalCollection.extend({
@@ -1405,11 +1410,13 @@ $(document).ready(function() {

var mammals = new MammalCollection( [
{ id: 5, species: 'chimp', type: 'primate' },
{ id: 6, species: 'panther', type: 'carnivore' }
{ id: 6, species: 'panther', type: 'carnivore' },
{ id: 7, species: 'person', type: 'human' }
]);

ok( mammals.at( 0 ) instanceof scope.Primate );
ok( mammals.at( 1 ) instanceof scope.Carnivore );
ok( mammals.at( 2 ) instanceof scope.Human );
});

test( "Object building based on type, when used in relations" , function() {
@@ -1422,8 +1429,13 @@ $(document).ready(function() {
'dog': 'Dog'
}
});
var Dog = scope.Dog = PetAnimal.extend();
var Dog = scope.Dog = PetAnimal.extend({
subModelTypes: {
'poodle': 'Poodle'
}
});
var Cat = scope.Cat = PetAnimal.extend();
var Poodle = scope.Poodle = Dog.extend();

var PetPerson = scope.PetPerson = Backbone.RelationalModel.extend({
relations: [{
@@ -1445,21 +1457,30 @@ $(document).ready(function() {
{
type: 'cat',
name: 'Whiskers'
},
{
type: 'poodle',
name: 'Mitsy'
}
]
});

ok( petPerson.get( 'pets' ).at( 0 ) instanceof Dog );
ok( petPerson.get( 'pets' ).at( 1 ) instanceof Cat );
ok( petPerson.get( 'pets' ).at( 2 ) instanceof Poodle );

petPerson.get( 'pets' ).add({
petPerson.get( 'pets' ).add([{
type: 'dog',
name: 'Spot II'
});
},{
type: 'poodle',
name: 'Mitsy II'
}]);

ok( petPerson.get( 'pets' ).at( 2 ) instanceof Dog );
ok( petPerson.get( 'pets' ).at( 3 ) instanceof Dog );
ok( petPerson.get( 'pets' ).at( 4 ) instanceof Poodle );
});

test( "Automatic sharing of 'superModel' relations" , function() {
var scope = {};
Backbone.Relational.store.addModelScope( scope );
@@ -1484,6 +1505,10 @@ $(document).ready(function() {
scope.Flea = Backbone.RelationalModel.extend({});

scope.Dog = scope.PetAnimal.extend({
subModelTypes: {
'poodle': 'Poodle'
},

relations: [{
type: Backbone.HasMany,
key: 'fleas',
@@ -1493,22 +1518,33 @@ $(document).ready(function() {
}
}]
});
scope.Poodle = scope.Dog.extend();

var dog = new scope.Dog({
name: 'Spot'
});

var poodle = new scope.Poodle({
name: 'Mitsy'
});

var person = new scope.PetPerson({
pets: [ dog ]
pets: [ dog, poodle ]
});

ok( dog.get( 'owner' ) === person, "Dog has a working owner relation." );
ok( poodle.get( 'owner' ) === person, "Poodle has a working owner relation." );

var flea = new scope.Flea({
host: dog
});

var flea2 = new scope.Flea({
host: poodle
});

ok( dog.get( 'fleas' ).at( 0 ) === flea, "Dog has a working fleas relation." );
ok( poodle.get( 'fleas' ).at( 0 ) === flea2, "Poodle has a working fleas relation." );
});

test( "Overriding of supermodel relations", function() {