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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:06:03  来源:igfitidea点击:

Remove an attribute from a Backbone.js model

javascriptbackbone.js

提问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 属性中删除它,但它并没有真正删除它。

回答by Vincent Briglia

Are you looking for model.unset?

你在找model.unset吗?

Remove an attribute by deleting it from the internal attributes hash. Fires a "change" event unless silent is passed as an option.

通过从内部属性哈希中删除属性来删除属性。除非作为选项传递无声,否则触发“更改”事件。

You can find the documentation here.

您可以在此处找到文档。