Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 556
jaykumarark Avatar asked Dec 04 '22 13:12

jaykumarark


1 Answers

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.

like image 102
Pramod Avatar answered Dec 27 '22 10:12

Pramod