where to get the record id when calling backbone.js destroy
Asked Answered
W

2

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.

Worrywart answered 14/1, 2012 at 3:58 Comment(2)
could you share your test code please?Ledbetter
i just edited the post to add code. thanks for looking!Worrywart
L
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
Ledbetter answered 16/1, 2012 at 2:5 Comment(3)
How does the null id turn into 1?Paint
@Paint when creating a new instance with this line new AccountModel({id: 1, username: 'test'});Ledbetter
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.Chamade
I
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.

Interesting answered 14/1, 2012 at 13:1 Comment(2)
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.Worrywart
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 idDarsie

© 2022 - 2024 — McMap. All rights reserved.