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.
You could bind the save method of your collection to the add event:
MyCollection = Backbone.Collection.extend({
initialize: function(){
this.bind('add', this.save, this)
}
save: function(){
$.post(this.url, this.toJSON())
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With