backbone.jsbackbone.js-collectionsbackbone-model

add updated model into Backbone Collections


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.

Do I need to extend the collection create method? Or is there another way?


Solution

  • 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});