简体   繁体   English

从Backbone.js模型中删除属性

[英]Remove an attribute from a Backbone.js model

Is there a way to remove an attribute from a Backbone model? 有没有办法从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 set method 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属性中删除它,但它并没有真正删除它。

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 . 你可以在这里找到文档。

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

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