Ben
Ben

Reputation: 5414

Backbone - receiving DELETE data in the back end

Say you have a model with an id, and you want to delete it in the database. So you call the destroy() method on that model (as below in the code example). That sends an OPTIONS HTTP request, followed by a DELETE HTTP request. My issue is that while I'm catching the DELETE request nicely on the server side, I can't find any information telling me what the model id is - it's not a parameter in the request and it's not in the URL. How do I find this information? I can't see it in the documentation here. Here is a link to the repo where I'm storing the code.

  removeElement: function() {
    // DELETE in DB
    this.model.destroy();
    this.remove();
    this.unbind();
  },

What I would expect is that the HTTP request would have a param like { 'id': 42319 } or some such.

Upvotes: 0

Views: 68

Answers (2)

Ben
Ben

Reputation: 5414

My problem was that I was defining the url attribute in BOTH the model AND the collection. You should only define url in the collection. Stupid mistake.

Upvotes: 0

cs_stackX
cs_stackX

Reputation: 1527

You probably need to set the model ID attribute. The id set automatically by Backbone on the client is cid and not id. Note that a common gotcha with DBs that use a different unique key (like MongoDB) is not mapping from that key to ID as described in the docs

For example:

var Model = Backbone.Model.extend({
    idAttribute: "_id"
    //other model setup code
});

Upvotes: 1

Related Questions