Search code examples
backbone.js

Automatically save after adding model to collection


I have a collection myCollection to which I add models as follows:

myCollection.add({title: Romeo and Juliette, author: Shakespear});

Have can I now save this added model to the server? Backbone Collections do not have a save() and I do not a reference to the added model to call save directly.


Solution

  • You can use the create function on the collection to add a model and have it automatically saved to the server.

    myCollection.create({title: Romeo and Juliette, author: Shakespeare});
    

    Here's the documentation on the create function.