0

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

Harry
  • 13,091
  • 29
  • 107
  • 167

2 Answers2

3

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

Alex Polkhovsky
  • 3,340
  • 5
  • 29
  • 37
  • I would try removing localStorage attribute for Load model. Leave it for Loads collection. – Alex Polkhovsky May 17 '13 at 22:16
  • Remvoed it, still the same. I just added that to try somehting ellse – Harry May 19 '13 at 10:51
  • Everything looks all right to me, except the fact that you have localStorage and urls configured simultaneously. Indulge me once more please and remove localStorage lines altogether. Load should still have urlRoot configured. Loads should have url. Thanks. – Alex Polkhovsky May 20 '13 at 14:14
-1

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.

Brendan Delumpa
  • 1,155
  • 1
  • 6
  • 11
  • Oops.. yes.. Oh well... Is your server expecting some sort of payload for the delete? Or is it one of old-style servers that you have to do a POST to do a delete? – Brendan Delumpa May 17 '13 at 20:20