Вопрос

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.

Это было полезно?

Решение

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

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top