Dre*_*ams 3 javascript model-view-controller jquery backbone.js
使用Backbone,我正在尝试更新并向服务器保存一个属性:
currentUser.save({hide_explorer_tutorial: 'true'});
Run Code Online (Sandbox Code Playgroud)
但我不想发送所有其他属性.其中一些实际上是服务器端方法的输出,因此它们实际上不是具有setter函数的true属性.
目前我正在使用unset(attribute_name)删除我不想在服务器上更新的所有属性.问题是这些属性不再可供本地使用.
有关如何仅将某些属性保存到服务器的建议?
vik*_*iky 20
截至Backbone 0.9.9
只需传递{patch:true}给save函数,如下所示:
currentUser.save({hide_explorer_tutorial: 'true'}, {patch:true});
Run Code Online (Sandbox Code Playgroud)
从文档中,
相反,如果您只想将更改的属性发送到服务器,请调用model.save(attrs,{patch:true}).只有传入的属性,您将收到服务器的HTTP PATCH请求.
您可以在模型上使用toJSON来执行此操作.
toJSON : function(){
return {hide_explorer_tutorial: this.get("hide_explorer_tutorial")};
}
Run Code Online (Sandbox Code Playgroud)
这将是保存时发送到后端的唯一属性.
| 归档时间: |
|
| 查看次数: |
9310 次 |
| 最近记录: |