Question

I'm trying to use a Backbone model in my Marionette application. I have a collection view which correctly displays all entries I have for a specific model. The item view contains a delete button. It listens for clicks on that button, calling a delete function when such a click occurs:

events: {
    'click .deleteItem': 'deleteItem'
},
deleteItem: function (event) {
    console.log('id', this.model.id);
    this.model.destroy({ wait: true });

}

this.model.destroy correctly make a DELETE request to the myModel/ url, but it doesn't seem to include any information on which entry is to be delete. For example, on the server side (a Node.js server), I don't see anything in req.body (yes, I'm using the body parser). How do I get the id of the entry I'm trying to delete when responding to the DELETE request on the server side?

Note: I can get it easily enough on the client side: this.model.id

Was it helpful?

Solution

Use the route parameters:

app.delete('/thing/:id', function(req, res){
    console.log('deleting ' + req.params.id); 
});

Then your model would look like this:

var ThingModel = Backbone.Model.extend({
    idAttribute: '_id', // or whatever your id attribute is
    urlRoot: '/thing'
});

Then you would delete it like so:

var someThing = new ThingModel({_id:123, foo:'bar'});
someThing.destroy();

It sounds like you did not declare the urlRoot of your model. Read up on it here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top