如何取消设置_id骨干模型属性? [英] how to unset an _id backbone model attribute?

查看:68
本文介绍了如何取消设置_id骨干模型属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从模型实例中 _id 属性 unset()使用以下命令发出 POST 请求 save()模型方法.

I would like to unset() the _id attribute from an instance of a model, to make a POST request using the save() model method.

但是我得到了一个 Uncaught TypeError:由于此行,对象[object Object]没有方法'call'bone-min.js :

myModel.unset('_id');

我正在使用 idAttribute:"_id" ,所以我尝试了:

I am using idAttribute: "_id" so i tried:

myModel.unset('id');

但是它不会取消设置 _id 属性.

But it doesn't unset the _id attribute.

推荐答案

使用 model.unset('_ id')应该可以正常工作.我的猜测是该错误是由您的代码或某些库代码中的 change 事件侦听器引发的.为了不触发事件,可以使用 silent:true 选项.

Using model.unset('_id') should work fine. My guess is that the error is thrown by a change event listener, either in your code or some library code. In order to not trigger events you can use the silent:true option.

但是,如果您只想强制 model.save()方法执行 POST ,则无需取消设置 _id 属性.

However, if you simply want to force the model.save() method to perform a POST, you don't need to unset the _id attribute.

相反,请覆盖 model.isNew 方法.Backbone使用它来确定模型是新模型(应为 POST )还是现有模型(应为 PUT ).覆盖始终返回true的方法将使您的模型每次都 POST :

Instead override the model.isNew method. Backbone uses this to determine whether a model is new (and should be POSTed) or existing (and should be PUT). Overriding the method to always return true will make your model to be POSTed every time:

isNew: function() { return true; }

这篇关于如何取消设置_id骨干模型属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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