7

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

touseefkhan4pk
  • 473
  • 4
  • 26
  • This question has been asked before. See for example: http://stackoverflow.com/questions/6879138/how-to-save-an-entire-collection-in-backbone-js-backbone-sync-or-jquery-ajax – Alex Nov 30 '12 at 12:26

1 Answers1

3

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

Community
  • 1
  • 1
Cubed Eye
  • 5,581
  • 4
  • 48
  • 64