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

Deep subModelTypes with custom subModelTypeAttribute #545

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 27 additions & 9 deletions backbone-relational.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@
* @param {Backbone.RelationalModel} modelType
*/
setupSuperModel: function( modelType ) {
_.find( this._subModels, function( subModelDef ) {
return _.filter( subModelDef.subModels || [], function( subModelTypeName, typeValue ) {
_.each( this._subModels, function( subModelDef ) {
return _.each( subModelDef.subModels || [], function( subModelTypeName, typeValue ) {
var subModelType = this.getObjectByName( subModelTypeName );

if ( modelType === subModelType ) {
Expand All @@ -238,9 +238,8 @@
modelType._superModel = subModelDef.superModelType;
modelType._subModelTypeValue = typeValue;
modelType._subModelTypeAttribute = subModelDef.superModelType.prototype.subModelTypeAttribute;
return true;
}
}, this ).length;
}, this );
}, this );
},

Expand Down Expand Up @@ -438,7 +437,7 @@
if ( coll ) {
var obj = coll.get( id );

if ( obj instanceof type ) {
if ( obj && obj._isSubModelInstanceOf( type )) {
return obj;
}
}
Expand Down Expand Up @@ -735,7 +734,7 @@
* @return {Boolean}
*/
_isReverseRelation: function( relation ) {
return relation.instance instanceof this.relatedModel && this.reverseRelation.key === relation.key &&
return relation.instance && relation.instance._isSubModelInstanceOf(this.relatedModel) && this.reverseRelation.key === relation.key &&
this.key === relation.reverseRelation.key;
},

Expand Down Expand Up @@ -1292,6 +1291,24 @@
return this;
},

/**
* Determine if the current object is an instance of a given Model or of one of its SubModels
* @param {Backbone.RelationalModel} type
* @return {Boolean}
*/
_isSubModelInstanceOf: function( type ) {
if ( this instanceof type ) {
return true;
}
return _.filter( type.prototype.subModelTypes || [], function( subModelTypeName, typeValue ) {
var subModelType = Backbone.Relational.store.getObjectByName( subModelTypeName );

if ( this._isSubModelInstanceOf(subModelType) ) {
return true;
}
}, this).length;
},

/**
* Initialize Relations present in this.relations; determine the type (HasOne/HasMany), then creates a new instance.
* Invoked in the first call so 'set' (which is made from the Backbone.Model constructor).
Expand Down Expand Up @@ -1728,9 +1745,10 @@
this.initializeModelHierarchy();

// Determine what type of (sub)model should be built if applicable.
var model = this._findSubModelType( this, attributes ) || this;

return new model( attributes, options );
var model = this._findSubModelType( this, attributes );
if (model)
return model.build.call(model, attributes, options);
return new this( attributes, options );
},

/**
Expand Down
40 changes: 40 additions & 0 deletions test/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1755,6 +1755,46 @@ $(document).ready(function() {
ok( mammals.at( 3 ) instanceof scope.Primate );
});

test( "Object building based on type in a custom field, when using explicit collections" , function() {
var scope = {};
Backbone.Relational.store.addModelScope( scope );

scope.Mammal = Animal.extend({
subModelTypes: {
'primate': 'Primate',
'carnivore': 'Carnivore'
}
});
scope.Primate = scope.Mammal.extend();
scope.Carnivore = scope.Mammal.extend({
subModelTypeAttribute: 'family',
subModelTypes: {
'feline': 'Feline',
'canid': 'Canid'
}
});

scope.Feline = scope.Carnivore.extend();
scope.Canid = scope.Carnivore.extend();

var MammalCollection = AnimalCollection.extend({
model: scope.Mammal
});

var mammals = new MammalCollection( [
{ id: 5, species: 'chimp', type: 'primate' },
{ id: 6, species: 'panther', type: 'carnivore' },
{ id: 9, species: 'coyote', type: 'carnivore', family: 'canid' },
{ id: 10, species: 'leopard', type: 'carnivore', family: 'feline'}
]);

ok( mammals.at( 0 ) instanceof scope.Primate );
ok( mammals.at( 1 ) instanceof scope.Carnivore );
ok( mammals.at( 2 ) instanceof scope.Canid );
ok( mammals.at( 3 ) instanceof scope.Feline );
});


test( "Object building based on type, when used in relations" , function() {
var scope = {};
Backbone.Relational.store.addModelScope( scope );
Expand Down