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 Collection s do not have a save() and I do not a reference to the added model to call save directly.


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.


您可以将集合的save方法绑定到add事件:

MyCollection = Backbone.Collection.extend({
    initialize: function(){
        this.bind('add', this.save, this)
    }
    save: function(){
        $.post(this.url, this.toJSON())
    }
})
链接地址: http://www.djcxy.com/p/67134.html

上一篇: 保存Backbone.js模型并更新整个集合

下一篇: 将模型添加到集合后自动保存