With Backbone (1.2.3), I'm trying to build a collection using multiple models using the collection's model
property:
var coll = Backbone.Collection.extend({
model: function(attrs, options) {
switch(attrs.type) {
case 'type-a':
return new BackboneModelA(attrs, options);
case 'type-b':
return new BackboneModelB(attrs, options);
}
},
modelId: function(attrs) {
return attrs.type + ':' + attrs.id;
}
});
new coll([
{type: 'type-a', id: 1}
]);
But it throws an error :
TypeError: Cannot read property 'type' of undefined(…)
Actually, modelId()
is called twice and the second time, attrs
is undefined
.
PS: I know it's quite a duplicate of this : Backbone Collection with multiple models? but I haven't found anything helpful there.