8

I have some test code that i'm just trying to use to figure out backbone.js. when i call destroy on a model object backbone makes a DELETE request to my rest service. but, i can't see any ID indicating which record is being deleted in the request data, the querystring, body or anywhere.

my model has an id property and i've assigned it a value of 1. is there anything else that i have to do to make sure the id gets passed through the server? or is there some other way that i'm supposed to detect what record is being deleted?

Edit - Here's the relevant code:

var AccountModel = Backbone.Model.extend({
    url: 'Account/Update',
    id: null,
    username: ''
});

var accountM = new AccountModel({id: 1, username: 'test'});

accountM.destroy();

When I look at the debugger I see the AJAX request is made, it just looks like this:

Request URL:http://localhost/probli/Account/Update
Request Method:DELETE
Status Code:200 OK

There doens't seem to be an ID or anything and there's no post data. Am I doing anything wrong? Thanks.

Jason
  • 1,726
  • 17
  • 19

2 Answers2

17

You should set the urlRoot attribute of your model then let Backbone handle constructing the DELETE url:

var AccountModel = Backbone.Model.extend({
    urlRoot: 'Account/Update',
    id: null,
    username: ''
});

This will cause the following request when accountM.destroy() is called:

Request URL:http://localhost/probli/Account/Update/1
Request Method:DELETE
Status Code:200 OK
Bobby
  • 18,217
  • 15
  • 74
  • 89
  • How does the null id turn into 1? – lulalala Sep 24 '12 at 08:34
  • @lulalala when creating a new instance with this line `new AccountModel({id: 1, username: 'test'});` – Bobby Sep 24 '12 at 14:09
  • 2
    I'd also add that you *only* need urlRoot. If you have both url and urlRoot, you may experience this issue, getting rid of url should clear it up. – Jeb Jan 06 '13 at 09:11
1

Backbone.sync will send the destroy back as a simple request with the ID in the url. For example:

DELETE http://example.com/foos/1

This is the HTTP delete for a Foo with an id of 1.

In MVC web servers like Rails, ASP.NET MVC, and even Sinatra and other simple servers, this will be a parameter that comes through to your server.

For example, in Sinatra:


delete "/foos/:id" do

  id = params[:id] # the id from the url/route

  foo = Foos.find(id) # get the foo
  foo.destroy
  return {}.to_json
end

As you can see, I defined a parameter in the route, called :id and my code was able to access it via the params data. I then found the Foo in question, destroyed it, and returned an empty JSON result - which is required by Backbone, even during a destroy.

Hope that helps.

Derick Bailey
  • 72,004
  • 22
  • 206
  • 219
  • thanks for the reply. is there somewhere in the backbone javascript that you specify where the id will be added to the url? I just posted some sample code and looking at the dubugger i can see the request being made but there's no id. thanks. – Jason Jan 16 '12 at 01:33
  • Any update to this. I'm having the same issue. DELETE request is sent but `id` is not sent in the URL. I've checked the model does exist in the collection with a valid id – detj Sep 27 '12 at 13:52