1

With the current ember-data 0.13 is there a way to update a model from the response content of a PUT request?

In my REST api fields (such as updated_at) are set at the server during an object update and the client model needs to be updated with the content of the response from the PUT request.

Specifically, an updated_at property that's used for concurrency handling needs to be set from the PUT response.

JohnC
  • 3,095
  • 4
  • 34
  • 48
  • ember-data should update your model based on response to content of a PUT request. Can you post a failing example? Also check out http://stackoverflow.com/questions/14922623/what-is-the-complete-list-of-expected-json-responses-for-ds-restadapter/14945452#14945452 maybe something there will help – Mike Grassotti Jul 04 '13 at 03:53
  • Thank you Mike, I didn't know it was *supposed* to update, I must be doing something wrong. Cheers! – JohnC Jul 04 '13 at 14:25
  • Cool. I looked around and couldn't find a test that verifies this behavior so could be you've uncovered a bug. According to ember-data source it should work: https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/adapter.js#L225-L227 – Mike Grassotti Jul 04 '13 at 17:27
  • Thanks again for your help Mike, just knowing it *should* update was what I needed to know, the rest was breakpoints and tracing to pin it down. – JohnC Jul 04 '13 at 21:25

1 Answers1

3

Figured it out. For future reference if the REST API returns a payload as a result of a PUT request it must not be inside an array.

I.E. this:

{
  "client": {
    "__v": 2,
    "_id": "51d47b5b3f7499341a00006a",        
    "created_at": "2013-07-03T19:28:27.868Z",
    "created_by": "51d47b5b3f7499341a000003",
    "name": "A1",        
    "updated_at": "2013-07-04T21:20:36.311Z",
    "updated_by": "51d47b5b3f7499341a000004",
  }
}

And not this:

{
  "client": [
    {
      "__v": 2,
      "_id": "51d47b5b3f7499341a00006a",
      "created_at": "2013-07-03T19:28:27.868Z",
      "created_by": "51d47b5b3f7499341a000003",
      "name": "A1",
      "updated_at": "2013-07-04T21:23:11.943Z",
      "updated_by": "51d47b5b3f7499341a000004",
    }
  ]
}
JohnC
  • 3,095
  • 4
  • 34
  • 48