jaykumarark
jaykumarark

Reputation: 2449

backbone model.destroy not triggering a DELETE request

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){...});

Upvotes: 4

Views: 6590

Answers (1)

Pramod
Pramod

Reputation: 5208

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.

Upvotes: 16

Related Questions