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

≡放荡痞女 提交于 2019-12-25 06:58:45

问题


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, and then save it to the corresponding models, or something else. Ideas?


回答1:


Now it all makes sense to me. Of course, this was the problem in your last question. Anyway, ember-data's RESTAdapter can't handle this. If you're requesting a singular resource user it expects at most this user as a singular answer. Any other resource that may be "side-loaded" has to be an array. The requested singular user can either be a record under the user key or the first entry in an array unter the users key. Everything else (except meta data) has to be plural.

In case you're requesting a plural resource users, the main response must be under the users key, any sideloaded users that aren't part of the response prfixed with _ under the _users key.

For your example all this means that your json must be formatted like this:

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

       authentication-tokens: [{
                         token: "token",
                         id: "id"
       }]
}

If you can't change your server, you'd have to configure the RESTAdapter to normalize the JSON data through normalize of the Serializer.



来源:https://stackoverflow.com/questions/29607845/can-ember-data-handle-two-models-from-one-json-payload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!