本文介绍了覆盖backbone.sync只为认沽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法从数据库覆盖 PUT
响应同步法和排除 ID
字段的任何可能的方式,而其余的一样吗?我使用的原料药Django的REST框架;尝试 PUT
,我收到:
{field_errors:ID:此字段不存在]}
我想,如果我重写同步和排除 ID
字段 PUT
,我的问题将得到解决,但我不知道该怎么做。
解决方案
Backbone._sync = Backbone.sync;
Backbone.sync =功能(方法,模型,期权){
VAR PARAMS = _.clone(选件);
删除model.attributes.id;
params.success =函数(模型){
如果(options.success)options.success(模型);
};
params.error =函数(模型){
如果(options.error)options.error(模型);
};
Backbone._sync(方法,模型,则params);
}
Is there any possible way to override the sync method for PUT
response and exclude the id
field from db, and remaining the same? I am using the Django REST framework for APIs; while trying to PUT
, I'm getting:
{"field_errors": "id": ["This field does not exist."],}
I think if I override sync and exclude the id
field for PUT
, my problem will be solved, but I don't know how to do it.
解决方案
Backbone._sync = Backbone.sync;
Backbone.sync = function(method, model, options) {
var params = _.clone(options);
delete model.attributes.id;
params.success = function(model) {
if(options.success) options.success(model);
};
params.error = function(model) {
if(options.error) options.error(model);
};
Backbone._sync(method, model, params);
}
这篇关于覆盖backbone.sync只为认沽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!