从Backbone.js的模型删除属性 [英] Remove an attribute from a Backbone.js model

查看:142
本文介绍了从Backbone.js的模型删除属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法,以消除从骨干模型属性?

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

文档说不要直接编辑model.attributes,所以唯一的其他方法我认为这样做将是使用的设置方法,并设置为空的属性,但不是理想

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.

推荐答案

您是否在寻找 model.unset

从内部属性哈希删除它删除属性。
  触发一个change事件,除非是沉默作为一个选项过去了。

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.

这篇关于从Backbone.js的模型删除属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆