Search code examples
javascriptember.jsember-dataember-cli

Modifying Embers expected json api response


I'm new to Ember and from what I understand it has a very specific way it excepts its json api response to look like. Like so:

{
  "post": {
    "id": 1,
    "title": "Node is not omakase",
    "comments": [1, 2, 3]
  },

  "comments": [{
    "id": 1,
    "body": "But is it _lightweight_ omakase?"
  },
  {
    "id": 2,
    "body": "I for one welcome our new omakase overlords"
  },
  {
    "id": 3,
    "body": "Put me on the fast track to a delicious dinner"
  }]
}

Now the api I've already built has an json response that looks like so:

{
  "data": {
    "id": 1,
    "name": "Pansy Bednar",
    "links": [
      {
        "rel": "self",
        "uri": "/pansy-bednar15"
      }
    ],
    "players": {
      "data": [
        {
          "id": 2,
          "name": "Nicholas O'Reilly",
          "position": "cad",
          "age": 23,
          "value": "640",
          "links": [
            {
              "rel": "self",
              "uri": "/team/nicholas-o-reilly71"
            }
          ]
        }
      ]
    }
  }
}

The api is pretty big and is working fine with the mobile app. So a code re write would be to costly I would just choose another js framework even though I like Ember the best.

So my question is is there any way I can adapt the expected json response in ember. If yes how hard is it? Worth the time or should I just go for Angular or Aurelia.

or am I completely wrong and there is no one expected response to ember?


Solution

  • The thing you can do with ember is to write your very own adapter for this there are already many questions+answers out there:

    1. https://stackoverflow.com/a/17938593/1581725
    2. https://stackoverflow.com/a/24411550/1581725
    3. ...

    And there is also this blog entry here: http://eviltrout.com/2013/03/23/ember-without-data.html it's about using ember without ember-data.

    Found this little gem called normalizePayload - maybe this would also work for your case: https://stackoverflow.com/a/21790093/1581725