4

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

1
  • try putting "idAttribute: '_id'" as mentioned in the answer by pramod. Commented Dec 26, 2012 at 8:42

1 Answer 1

16

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.

Sign up to request clarification or add additional context in comments.

2 Comments

'idAttribute', write full attribute name, 'idAttr' might not work.
Woot thank you! After half an hour of searching this is by far the most elegant solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.