Can Ember-Data handle two models from one JSON payload?


#1

I have JSON coming from the server which looks like:

data: {
       user: {
              address: {
                         id: "id",
                         city: "city",
                         street: "street",
                         .......
              }
              name: "name",
              ......
       }

       authentication-token: {
                         token: "token",
                         id: "id"
       }
}

The idea is to store this two models (user, authentication-token) in ember store under the same names. When I gat the above mentioned response from a server, model user is saved successfully, but model authentication-token does not get saved to the store at all. When I log the data (in the adapter) before the data is passed to serializer I see that JSON has a structure which Ember-Data expects. I don’t know whether the problem is that Ember-Data cannot handle two models in success at one time, or something else. Ideas?


#2

I depends which serializer you’re using. I don’t think any of the standard serializers would be able to understand the format you’re trying to fetch, but it’s quite simple to write your own serializer (just extend the JSON or REST serializer which comes with Ember) and tell it to iterate through all top-level elements in the JSON payload and load them into the store.

You can also take a look at json:api which supports side-loaded models (and you can get community-made serializers for it).


#3

ember data supports sideloaded models by default, you definitely don’t need to write anything custom for this - just transform you json into the normalized form that ember data expects


#4

Correct, but not in the format he is expecting. Side-loaded relationships are represented as an array that lives outside the root.