简体   繁体   English

在骨干模型中保存多个记录?

[英]save multiple record in backbone model?

Am trying to save model in backbone. 我正在尝试将模型保存在骨干网中。 This code is working myModel.save({'title':title},{changed:'title'}); 这段代码正在运行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 {} . 要为单个模型保存多个属性,请将所有更改的属性及其对应的值包括在单个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 . 要在集合中的所有模型上保存属性,可以遍历集合,可能使用下划线的each函数

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. 注意:由于这些示例不在上下文中,因此必须使用其他逻辑来处理属性的唯一值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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