REST风格的前preSS猫鼬和放大器;主干 - 主干model.remove()不工作 [英] RESTful Express Mongoose & Backbone - Backbone model.remove() not working
问题描述
我正在开发使用防爆preSS,猫鼬和骨干与木偶节点的应用程序。
I'm developing a Node app using Express, Mongoose and Backbone with Marionette.
所有路由都运作良好,除了删除路由。
All routes are working well except the delete route.
如果我打电话this.model.destroy,我总是得到这样的错误:
If I call this.model.destroy, I always get this error:
DELETE http://localhost:3000/api/user 404 (Not Found)
404在防爆preSS的删除路线返回,就像如果防爆preSS不支持它,但我已经看到了整个网络的例子不胜枚举使用它。
The 404 is returned in Express's delete route, like if Express didn't support it, but I've seen numerous examples across the web using it.
下面是我的设置:
猫鼬架构:
var UserSchema = new mongoose.Schema(
{
name: String,
email: String,
age: Number
});
User = mongoose.model('User', UserSchema);
防爆pressJS路线:(不工作)
ExpressJS Route: (not working)
app.del('/api/user/:id', user.remove);
或
app.delete('/api/user/:id', user.remove);
这条航线由骨干model.destroy(所谓的),但返回404错误。
This route is called by backbone model.destroy(), but returns error 404.
防爆pressJS user.js的控制器:(因为之前的404工程,但没有达到)
ExpressJS user.js controller: (works but is not reached because of the 404 before)
exports.remove = function(req, res)
{
var id = req.params.id;
User.findById(req.params.id, function(err, user)
{
user.remove(function(err)
{
if(err) res.json(err);
res.json('all good');
});
});
};
BackboneJS型号
BackboneJS Model
var User = Backbone.Model.extend({
idAttribute: "_id",
url: '/api/user/',
});
BackboneJS客户端查看
BackboneJS client View
var UserView = Backbone.Marionette.ItemView.extend(
{
template: Handlebars.compile($('#userView').html()),
events:
{
'click .delete-button': 'deleteUser'
},
deleteUser: function(event)
{
this.model.remove();
}
});
我总是得到这个错误:
I always get this error:
DELETE http://localhost:3000/api/user 404 (Not Found)
但是它的工作原理,如果我用这个直接调用Ajax:
HOWEVER it works if I use this direct ajax call:
jQuery.ajax({
url:'/api/user/' + this.model.id,
type: 'DELETE',
success:function(data, textStatus, jqXHR)
{
}
});
那么,为什么这工作,如果我呼吁通过Ajax的路线,如果骨干网内部还使用Ajax?为什么骨干不能做出这样一个简单的model.destroy()?
So, why does this work if I call the route via Ajax, if Backbone internally also uses Ajax? Why does Backbone fail to make such a simple model.destroy()?
有没有一种方法来配置骨干Model.destroy方法很好地工作,如阿贾克斯上面的例子?谢谢
Is there a way to configure Backbone Model.destroy method to work well like the Ajax example above? Thanks
推荐答案
发现问题。骨干model.remove()没有发送ID,因为我以这种方式使用URL:
Found the problem. Backbone model.remove() was not sending the id because I was using "url" in this way:
Backbone.Model.extend({
url: '/users',
//...
});
这将告诉骨干力量使用完全相同/用户的URL进行所有操作。
That will tell Backbone to use exactly /users as the URL for all actions.
要确保使用URL发送的ID,可以使用一个功能:
To ensure sending the id using "url", one can use a function:
url: function() {
return '/list_items/' + encodeURIComponent(this.id)
}
甚至更好用urlRoot,而不是网址,让默认的网址功能添加ID:
Or even better use "urlRoot" instead of "url", let the default "url" function add the id:
urlRoot: '/users'
工作像urlRoot魅力
Working like a charm with urlRoot
这篇关于REST风格的前preSS猫鼬和放大器;主干 - 主干model.remove()不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!