简体   繁体   中英

save multiple record in backbone model?

Am trying to save model in backbone. This code is working myModel.save({'title':title},{changed:'title'});

But am not sure why it is not working without changed attribute. myModel.save({'title':title});

Also how do I save multiple records using this?

To save multiple attributes for a single model, include all the changed attributes and their corresponding values in a single object {} .

For example:

myModel.save({
  attributeA: valueA,
  attributeB: valueB,
  attributeC: valueC
});

It may help to read through backbone's annotated source regarding the save function .

To save attributes on all models in a collection, you could iterate over the collection, possibly using underscore's each function .

For example:

myCollection.each(function (myModel) {
  myModel.save({
    attributeA: valueA,
    attributeB: valueB,
    attributeC: valueC
  });
})

Note: Since these examples are out of context, unique values for the attributes would have to be handled with additional logic.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM