Javascript 从 Backbone.js 模型中删除属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10605730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove an attribute from a Backbone.js model
提问by MattoTodd
Is there a way to remove an attribute from a Backbonemodel?
有没有办法从Backbone模型中删除属性?
Reason being is I pass up extra data on save to perform certain actions, but then that data gets automatically added to my model
原因是我在保存时传递了额外的数据以执行某些操作,但是这些数据会自动添加到我的模型中
The documentation says to not edit the model.attributes directly, so the only other method I see to do this would be to use the setmethod and set the attribute to null, but that is not ideal
文档说不要直接编辑model.attributes,所以我看到的唯一其他方法是使用set方法并将属性设置为null,但这并不理想
var myModel = new Model()
myModel.save({name:'Holla', specialAttr:'Please Remove me'})
myModel.set({tempAttr:null})
if(myModel.attributes['specialAttr'] == null){
alert("Model does not have a specialAttr")
}
I've also tried removing it from the attributes property, but it doesn't really remove it.
我也试过从attributes 属性中删除它,但它并没有真正删除它。