9

I'm using Ember Data with the RESTful adapter with a rails backend.

When I delete a record from Ember record.deleteRecord(); record.save() the DELETE request goes to the server and the model is deleted, but this error is printed to the javascript console:

Extract requested, but no data given for App.ThisModel. This may cause weird problems.

The response from the server was just render json: true, so I changed it to render json: deleted_model which renders the json for the deleted record.

That got rid of the previous error, but now the deleted record is recreated in Ember.

What does Ember expect in the response?

everett1992
  • 2,351
  • 3
  • 27
  • 38

3 Answers3

7

You should send back a 200 with an empty valid json response {}, any data returned is applied to the record as if they were attributes.

http://emberjs.jsbin.com/OxIDiVU/215/edit

Additionally you can send back a 204 with no response.

http://emberjs.jsbin.com/OxIDiVU/214/edit

Kingpin2k
  • 47,277
  • 10
  • 78
  • 96
  • Your answer is right, but I'm a little curious, what would ember do with the attributes? If a delete request returns 200 the resource should have been removed, what would ember do with attributes for a deleted resource? – everett1992 Jan 03 '14 at 19:20
  • It would apply them to the record, then mark it as deleted, kin of silly, but Ember Data's _commit method is generalized to handle create/update/delete. – Kingpin2k Jan 03 '14 at 20:01
5

jQuery 1.9 no longer treats a response of 200 for a JSON request as a success. Your server should now be returning a 204 response for DELETE requests with empty response body.

For a rails server, you can do something like this:     

def destroy
  @something.destroy!
  head :no_content
end
Sapan Diwakar
  • 10,480
  • 6
  • 33
  • 43
0

In ember 1.5.1 and ember-data 1.0.0-beta.9+canary.410d62d6, I tried to return a empty json object/array, like { }, like in the answer above. It didn't work.

It only worked with a 200 response code and null as response value.

So maybe that has changed, or it depends on other things as well... I can't say, but only tell you what worked for me.

Preexo
  • 2,102
  • 5
  • 29
  • 37