I used collection create
method to save the model into the server. But this create
method automatically adds the stale model
into the collection
.
I am trying to do the following things.
After the inserting process success, I would like to add updated
model
into the collection. The updated info is returned from server
as a json
.
If the server responses error
, I don't want to add the model
into the
collection
.
Do I need to extend the collection create
method? Or is there another way?
From the docs:
Creating a model will cause an immediate "add" event to be triggered on the collection, a "request" event as the new model is sent to the server, as well as a "sync" event, once the server has responded with the successful creation of the model. Pass {wait: true} if you'd like to wait for the server before adding the new model to the collection.
(emphasis mine)
That'll look like:
collection.create({ // attributes
},{wait: true});