backbone model.destroy not triggering a DELETE request

Problem

This is my model.

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

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/books/:id', function(req, res){...});

Problem courtesy of: jaykumarark

Solution

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', 
    }

});

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.

Solution courtesy of: Pramod

Discussion

There is currently no discussion for this recipe.

This recipe can be found in it's original form on Stack Over Flow.