骨干model.destroy不会触发DELETE请求 [英] backbone model.destroy not triggering a DELETE request

查看:139
本文介绍了骨干model.destroy不会触发DELETE请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的模型。

library.BookModel = Backbone.Model.extend({
    urlRoot: '/api/books',
    defaults: {
        id      : null,
        imageurl: 'noimage.jpg', 
    }
});

我想发出调用book.destroy删除请求。但它不触发请求。当应用程序被初始化。该模型填充了来自服务器的数据。有在模型中设置两个ID属性。 ID (客户端ID)和 _id (MongoDB的ID)。我在初始化模式初始化函数的客户端ID作为book.attributes.id = book.attributes._id。所以一切都是设定。但我似乎无法启动删除请求。请告诉我,我要去哪里错了。我缺少什么吗?

I'm trying to issue a delete request by calling book.destroy. But it is not triggering the request. When the app is initialized. The model is populated with data from server. There are two IDs attributes set in the model. id(client side id) and _id(mongodb id). I initialized the client side id as book.attributes.id = book.attributes._id in model initialize function. So Everything is set. But i can't seem to initiate the delete request. please tell me where I'm going wrong. Am I missing anything here?

我的后端路由器被定义为这样的处理删除请求。

my backend router is defined as such to handle delete request.

app.delete('/ API /书/:身份证',函数(REQ,RES){...});

推荐答案

请尝试使用id属性 idAttribute 这对于你的情况(MongoDB中)为 _id 。这是ID服务器设置。

Try setting the id attribute using idAttribute which for your case (mongodb) is _id. This is the id set by the server.

library.BookModel = Backbone.Model.extend({

    urlRoot: '/api/books',

    idAttribute: '_id',

    defaults: {
        imageurl: 'noimage.jpg', 
    }

});

由骨干在客户机上自动设置的ID是 CID 并没有ID。可用于CID直到模型同步的服务器上,并获得一个服务器ID。

The id set automatically by Backbone on the client is cid and not id. cid can be used until the model is synced on the server and gets a server id.

这篇关于骨干model.destroy不会触发DELETE请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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