无法使服务器上更新一个id [英] Unable to make an update on the server with an id
问题描述
我更新的形式,我想使对serverwith一个id的更新请求
我的模型是:
I am updating a form and i want to make an update request on the serverwith an id my model is:
var CampaignEditModel = Backbone.Model.extend({
urlRoot:"http://localhost:3033/campaign/update/",
url : function(){
var url = this.urlRoot + this.id;
return url;
},
idAttribute: "_id",
defaults:{
"id":null ,
"Name" :""
}
});
渲染功能,这里所说的:
render function is called here:
$contents.empty().append(new EditView({model:editCampaigns}).render({id:id}).el);
和渲染功能是:
render: function(options){
this.$el.append( _.template(EditTemplate));
this.model.set({"id":options.id})
console.log(this.model.get("id"));
this._modelBinder.bind(this.model, this.el);
return this;
},
events: {
'click .saveCampaign ': 'save'
},
save:function(){
this.model.set({
"Name" :$('#edname').val(),
});
this.model.save(null, {success: function(data){
console.log("data:" + data);
require(['campaignroute'],function(routes){
var router = routes.pageRouter;
router.navigate('gridView', {trigger: true});
});
}});
return false;
}
问题连我都在模型中设置一个ID时仍然保存方法被调用
要求是这样的
the problem is even i have set an id in the model still when save method is called the request go like this
http://localhost:3033/campaign/update/undefined
和控制台显示eror:
and console shows the eror:
Failed to load resource: the server responded with a status of 404 (Not Found)
如何解决这个问题呢?
how to solve this problem?
推荐答案
渲染(选项)
的功能和设置模式 ID
还有,设置它直接在 editCampaigns
模式,进入前渲染(选项)
:
Instead of passing options
to your custom render(options)
function and setting the model id
there, set the it directly on the editCampaigns
model, before entering render(options)
:
editCampaigns.set('id', id);
$contents.empty().append(new EditView({model:editCampaigns}).render().el);
和删除多余的
this.model.set({"id":options.id})
从渲染(选项)
与选项一起
参数。它应该看起来像类似于这样:
from render(options)
together with the options
parameter. It should look like similar to this:
render: function(){
this.$el.append( _.template(EditTemplate));
console.log(this.model.get("id"));
this._modelBinder.bind(this.model, this.el);
return this;
}
您模式也有一个额外的网址
功能:
Your model also has an extra url
function:
url : function(){
var url = this.urlRoot + this.id;
return url;
}
您不需要这个,因为模型 ID
在 urlRoot
将自动添加。
you don't need this one since the models' id
is automatically appended after urlRoot
.
的无关的给你的问题,我看你用
Unrelated to you problem I see you used
http://localhost:3033/campaign/update
来定义您的更新网址。您使用的HTTP方法,已经说的动作会被执行什么,这就是为什么你可以(也应该)写网址没有动词的原因。只是删除多余的 /更新的
下面是关于最佳实践的快速摘要:
如何在没有动词创建REST的网址?
Here is a quick summary about best-practices: How to create REST URLs without verbs?
这篇关于无法使服务器上更新一个id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!