Harry
Harry

Reputation: 13329

destroy() does not send DELETE request

I know this has been asked many times but I cannot find the solution to my issue. In all the other questions the answers had to do with the ID not being set or being something else like _id etc.

I dont have any custom sync or destroy methods. Everything is standard.

My model and Collection:

class Load extends Backbone.Model
    localStorage: new Backbone.LocalStorage 'relodr.load'
    urlRoot: relodr.api.url + "loads/"
class Loads extends Backbone.Collection
    model = Load
    localStorage: new Backbone.LocalStorage 'relodr.loads'
    url: relodr.api.url + "loads/"

This is my bit where I want to delete the model:

delete: (e) =>
        f = $(e.currentTarget)
        id = $(f.parent()).attr("data-id") 
        load = new Loads()
        load.fetch()
        model = load.get id
        console.log model
        model.destroy()

The model is deleted from the collection, but the DELETE was never sent to the server.

This is the console.log model:

Backbone.Model {cid: "c5", attributes: Object, collection: Loads, _changing: false,     _previousAttributes: Object…}
    _changing: false
    _events: Object
    _pending: false
    _previousAttributes: Object
    attributes: Object
    changed: Object
    cid: "c5"
    id: 2
    __proto__: Object

SO I have a ID for the model, my model does have a URL set.

If I do this:

model = new Load
    id: 99
model.destroy()

It works, the DELETE request is sent although it return with a 500 error (obviasly). SO it tells me the urls etc are correct and working..

Just not sure why my destroy method is not working.

UPDATE

Interestingly when I log the model created above it looks like this:

Load {cid: "c6", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object…}

So the one said Load (name of the model) and the other Backbone.Model

Upvotes: 0

Views: 1541

Answers (2)

Brendan Delumpa
Brendan Delumpa

Reputation: 1145

I ran into this recently. This isn't a Backbone issue, but a jQuery issue. jQuery issue defaults to a dataType of 'json,' and it's expecting a JSON payload in the data, so when you issue the call, jQuery actually farts on the DELETE.

To get around this, try doing your delete with some options that'll be passed to jQuery.ajax:

model.delete({
    type: 'text',
    contentType : 'text/plain'
});

I found that that works like a charm.

Upvotes: -1

Alex Polkhovsky
Alex Polkhovsky

Reputation: 3360

My guess is you probably didn't set up urlRoot property for your model.

Upvotes: 3

Related Questions