48

I am attempting to write a custom express.js based server for an Ember.js app. I am getting along fairly well but I'm constantly getting stuck trying to guess what JSON responses Ember Data is expecting at a given moment.

This brand new documentation is a great start http://emberjs.com/guides/models/the-rest-adapter/ but not complete enough.

My stabbing in the dark has lead me to understand (Ember pre4, Ember Data 11):

Context                                Server URL          Method     Req. Data                  Resp. Data
~~~~~~~                                ~~~~~~~~~~          ~~~~~~     ~~~~~~~~~                  ~~~~~~~~~~
Getting a list of all users            /users              GET                                   {"users":[{...},{...}]}
Getting a particular user              /users/123          GET                                   {"user":{...}}
Creating a user                        /users              POST       {"user":{...}}             ???
Updating a user                        /users/123          PUT        {"user":{...}}             ???
Deleting a user                        /users/123          DELETE     ???                        ???

Creating a user (bulkUpdate)           /users              POST       {"users":[{...},{...}]}    ???
Updating a user (bulkUpdate)           /users/bulk         PUT        {"users":[{...},{...}]}    ???
Deleting a user (bulkUpdate)           /users/123          DELETE     ???                        ???

Can someone help me fill in some of these blanks?

Edit, the complete list of expected JSON responses

These responses were gleaned from the ember-data REST adapter unit tests and by watching the network traffic on the Example Ember Data app.

Context                                Server URL          Method     Req. Data                  Resp. Data
~~~~~~~                                ~~~~~~~~~~          ~~~~~~     ~~~~~~~~~                  ~~~~~~~~~~
Getting a list of all users            /users              GET                                   {"users":[{...},{...}]}
Getting a particular user              /users/123          GET                                   {"user":{...}}
Creating a user                        /users              POST       {"user":{...}}             {"user":{...}}
Updating a user                        /users/123          PUT        {"user":{...}}             {"user":{...}}
Deleting a user                        /users/123          DELETE     N/A                        null

Creating a user (bulkCommit)           /users              POST       {"users":[{...},{...}]}    {"users":[{...},{...}]}
Updating a user (bulkCommit)           /users/bulk         PUT        {"users":[{...},{...}]}    {"users":[{...},{...}]}
Deleting a user (bulkCommit)           /users/bulk         DELETE     {"users":[1,2]}            {"users":[1,2]}
James Andres
  • 1,532
  • 14
  • 20
  • So, based on this post and other research. I wrote this gist to convey most of the cases. Please leave feedbacks. Thanks. https://gist.github.com/ldong/c52f491a88935a8c24337c1326586f99 – Alan Dong Aug 04 '17 at 23:03

2 Answers2

25

Instead of stabbing in the dark, have a look at rest-adapter-test

For example, to fill in your question on response data for bulk updates, L738 describes the expected response data:

ajaxHash.success({ people: [
  { id: 1, name: "Brohuda Brokatz" },
  { id: 2, name: "Brocarl Brolerche" }
]});
Weston
  • 2,593
  • 1
  • 24
  • 32
Mike Grassotti
  • 19,010
  • 3
  • 57
  • 57
  • Woah, I sure wish I had seen that sooner. Thank you Michael! – James Andres Feb 19 '13 at 02:05
  • 1
    Updated out-of-date link. If it moves in future, basically just go to ember-data project on github and search project for rest_adapter_test.js - https://github.com/emberjs/data/blob/master/packages/ember-data/tests/integration/adapter/rest_adapter_test.js – Mike Grassotti Nov 12 '13 at 09:41
  • @MikeGrassotti: How is the standard called on which the format of the json requests / replies for ember-data is based? Is there a full specification of the standard somewhere? Does ember-data implement that standard fully? – blueFast Sep 25 '14 at 12:21
  • this answer is old, file changed its name and example doesn't exit any more. Anyone could please write where to find examples of the response in a https://github.com/emberjs/data/blob/master/packages/ember-data/tests/integration/adapter/rest-adapter-test.js ? – Marecky Aug 21 '15 at 11:47
  • 2017 Link: https://github.com/emberjs/data/blob/master/tests/integration/adapter/rest-adapter-test.js – Alan Dong Jul 15 '17 at 20:56
0

Currently look for phrases ajaxResponse( in rest-adapter-test.js source nested in test("create - a payload with a new ID and data applies the updates" look alike blocks to read required response of server.

Marecky
  • 1,424
  • 1
  • 19
  • 35