Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save collection of models in backbone.js?

I am using backbone in a project. I have a scenario where I have added my multiple models in a collection. Now i have array of models in the collection. I am using rest serivces in order to save. Now I want to make a post call to the server to save those array of models. I know there is a method to pass model to create method of collection like

this.collection.create(model)
But it is for single model to save. I had also tried to apply loop but it doesn't seem to work. Please help me out.

Thanks in advance

like image 435
touseefkhan4pk Avatar asked Nov 30 '12 11:11

touseefkhan4pk


1 Answers

You need to use the .add() function

this.collection.add(model);
this.colleciton.add([model1, model2, ..., modeln]);

Edit:

To save the models that are in the collection, you will need to write a function to iterate through them and save them all individually.

There are StackOverflow questions the answer this pretty well, for example:

"How" to save an entire collection in Backbone.js - Backbone.sync or jQuery.ajax?

Best practice for saving an entire collection?

How to save a Collection with backbone.js

like image 88
Cubed Eye Avatar answered Sep 25 '22 08:09

Cubed Eye