Remove an attribute from a Backbone.js model

点点圈 提交于 2020-01-12 11:07:33

问题


Is there a way to remove an attribute from a Backbone model?

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

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.


回答1:


Are you looking for 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.



来源:https://stackoverflow.com/questions/10605730/remove-an-attribute-from-a-backbone-js-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!