简体   繁体   English

将JSON序列化为Ember-Data(有效负载中的两个不同模型)

[英]Serialize JSON into Ember-Data (two different models in payload)

I have JSON coming from the server which looks like: 我有来自服务器的JSON,如下所示:

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

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

In Ember I have a model auth which should represent the authentication model that I get from the server. 在Ember中,我有一个模型auth ,它应该代表我从服务器获得的身份验证模型。 Since I have different names for same model on the server and in the Ember store, I wrote a serializer with one method typeForRoot(root) where I just map server authentication to Ember auth . 由于我在服务器上和Ember商店中对于同一模型使用不同的名称,因此我使用一种方法typeForRoot(root)编写了一个序列化程序,在其中我仅将服务器身份验证映射到Ember auth Problem is that this typeForRoot is actually never invoked, and I do not have a slightly idea why. 问题是实际上从未调用过这个typeForRoot,我也不知道为什么。

Here is Ember auth model: 这是Ember授权模型:

import DS from 'ember-data';

export default DS.Model.extend({
    token: DS.attr('string')

});

And here is my serializer: 这是我的序列化器:

import DS from 'ember-data';
import Ember from 'ember';

export default DS.RESTSerializer.extend({

  typeForRoot: function(root) {
    // 'authentication' should become 'auth'
    var subRoot = root.substring(0, root.length - 10);

    // _super normalizes 'authentication' to 'auth'
    return this._super(subRoot);
  }
});

My user model is being properly saved in the store (I'm using a separate, model based, serializer for user), but I get the message WARNING: Encountered "authentication" in payload, but no model was found for model name "authentication" (resolved model name using shop-app@serializer:user:.typeForRoot("authentication")) when authentication model needs to be saved in auth model in the store. 我的用户模型已正确保存在存储中(我正在为用户使用单独的,基于模型的序列化程序),但我收到消息WARNING: Encountered "authentication" in payload, but no model was found for model name "authentication" (resolved model name using shop-app@serializer:user:.typeForRoot("authentication"))当需要将认证模型保存在商店中的认证模型中时WARNING: Encountered "authentication" in payload, but no model was found for model name "authentication" (resolved model name using shop-app@serializer:user:.typeForRoot("authentication"))

Does anyone know how can I overcome this problem. 有谁知道我该如何克服这个问题。 Thanks, Milan 谢谢,米兰

I assume the typeForRoot you've implemented is on the auth serialiser? 我假设您实现的typeForRoot在auth序列化程序上? If you notice the error, it states: 如果您注意到该错误,则说明:

shop-app@serializer:user:.typeForRoot("authentication")

This particularly points to the User serialiser. 这尤其指向用户序列化程序。

I assume you're doing a request for the User model which returns the User model and Auth model in the data - since your using the User adapter and Serialiser to do this, you would need to implement the typeForRoot logic on the User serialiser. 我假设您正在对用户模型进行请求,该请求将在数据中返回用户模型和Auth模型-由于您使用用户适配器和序列化程序来执行此操作,因此您需要在用户序列化程序上实现typeForRoot逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM