I use Backbone with Marionette and backbone-relational.
Let's say I have a zoo with a Backbone.Collection
of animals.
I have one animal stored in the database (with the id 1).
Now I add a second animal to the Backbone.Collection and call zoo.save({}, {wait: true})
.
In response my backend server returns both animals (the old one with id 1 and the new one with id 2).
That works.
The problem: After saving the Backbone.Collection
contains three models.
- an animal with id 1
- an animal without an id (just a cid)
- a new animal with id 2 as returned by the server
Instead I only want two models:
- an animal with id 1
- an animal with id 2
How can I tell Backbone to set the server id at the already existing animal instead of adding a new model to the collection?
Additional information:
My Zoo model extends Backbone.RelationalModel
with relations defined as follows (similar to the example at http://backbonerelational.org/#RelationalModel-relations):
relations: [
type: Backbone.HasMany
relatedModel: 'Animal'
collectionType: 'AnimalCollection'
key: 'animals'
]
That's why saving the zoo will update the collection as well.