Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backbone.js DELETE request not firing

I'm trying to get the backbone.js DELETE request to fire, but don't see any requests being made in my console.

I have collection model like so:

var Model = Backbone.Model.extend(
{
    urlRoot: '/test',
    defaults:{}
});

var TableList = Backbone.Collection.extend(
{
    url: '/test',
    model: Model
});

In my view I'm running this:

this.model.destroy();

Everything seems to be running fine, I can see output coming from the remove function that calls the destroy so I know it's getting there plus it also successfully runs an unrender method that I have. Can't see any requests being made to the sever though?

like image 825
Rob Avatar asked May 24 '12 20:05

Rob


3 Answers

If I am not mistaken, you have to have an id property on your model to ensure that it hits the correct url. IE if your model was...

var Model = Backbone.Model.extend({
    url: '/some/url'
});

var model = new Model({
    id: 1 
});
model.destroy(); // I THINK it will now try and DELETE to /some/url/1

Without an id it doesn't know how to build the url correctly, typically you'd fetch the model, or create a new one and save it, then you'd have a Url...

See if that helps!

like image 116
jcreamer898 Avatar answered Oct 05 '22 10:10

jcreamer898


I found the issue to my problem, thought not a solution yet. I'm not sure this is a bug with backbone or not, but I'm using ajaxSetup and ajaxPrefilter. I tried commenting it out and it worked. I narrowed it down to the ajaxSetup method and the specifically the use of the data parameter to preset some values.

like image 28
Rob Avatar answered Oct 05 '22 09:10

Rob


Have you tried using success and error callbacks?

this.model.destroy({
    success : _.bind(function(model, response) {
                  ...some code
              }, this),
    error : _.bind(function(model, response) {
                  ...some code
              }, this);
});

Might be instructive if you're not seeing a DELETE request.

like image 43
Brendan Delumpa Avatar answered Oct 05 '22 11:10

Brendan Delumpa