You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
My issue is that if one of the classes listed in subModelTypes is extended, and defines its own a set of subModelTypes as well, it doesn't get properly inflated from the JSON coming from the server.
I would expect zoo.get("animals").first() to be an instance of Dog but its actually an instance of Animal.
I did some poking around a bit and I found that the Backbone.RelationalModel.build function is only searching the subModelTypes defined in the top-level class. I'm thinking that it should probably check the subModelType attributes of its entire class hierarchy too:
Backbone.RelationalModel = Backbone.RelationalModel.extend({}, {
build: function (attributes, options) {
// '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'.
var model = this._findSubModelType(this, attributes) || this;
return new model(attributes, options);
},
_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) {
return subModelType;
} else {
for (subModelTypeAttribute in type._subModels) {
return this._findSubModelType(type._subModels[subModelTypeAttribute], attributes);
}
}
}
return null;
}
});
The text was updated successfully, but these errors were encountered:
I'd be happy to put together the tests and pull-request for adding this functionality. I just need to know if this would be a welcome addition if it is the right way to go about it.
I've run into an issue using
subModelTypes
for Polymorphic collections/HasMany.Here is a jsFiddle demonstrating my issue and the fix I came up with:
http://jsfiddle.net/jimbo/HVDpQ/
My issue is that if one of the classes listed in
subModelTypes
is extended, and defines its own a set ofsubModelTypes
as well, it doesn't get properly inflated from the JSON coming from the server.So if you're using the following classes:
If you then try to create a new Zoo like so:
I would expect
zoo.get("animals").first()
to be an instance ofDog
but its actually an instance ofAnimal
.I did some poking around a bit and I found that the
Backbone.RelationalModel.build
function is only searching thesubModelTypes
defined in the top-level class. I'm thinking that it should probably check thesubModelType
attributes of its entire class hierarchy too:The text was updated successfully, but these errors were encountered: