Closed flip111 closed 10 years ago
You may want to look at issue https://github.com/dhruvaray/backbone-associations/issues/121.
@flip111
I have a one-to-many relation, where i always need one item on the many side. If the server does not have this data (from a previous time) then an empty model should be inserted into the collection.
Your scenario can be handled easily like this
var Location = Backbone.AssociatedModel.extend({
});
var Project = Backbone.AssociatedModel.extend({
relations: [
{
type: Backbone.Many,
key: 'locations',
relatedModel: Location
}
],
defaults : {'locations':[{}]}
});
var p = new Project;
console.log(p.get('locations').length) //1
Hi, thanks for this solution.
Two remarks though.
But for my use case the proposed solution will do. thank you
Hi,
I have a one-to-many relation, where i always need one item on the many side. If the server does not have this data (from a previous time) then an empty model should be inserted into the collection. Taken the example from the documentation i try to do this:
I initialize it like this:
I couldn't find a related question about collection initialization. Perhaps i'm using the wrong pattern here? I really want to avoid having to send empty data from the server, but let the client side initialize new models instead.