【问题标题】:emberdata 1.13 convert json to JSONAPIemberdata 1.13 将 json 转换为 JSONAPI
【发布时间】:2015-07-06 14:01:54
【问题描述】:

我正在使用 Ember Data 1.13.3。我想使用 JSONAPISerializer 中的 normalizeResponse 将旧的 JSON 格式转换为新的 JSONAPI 格式。

例如,我的 json 来自像

这样的网络服务

{
  user: { id: 1, name: 'wecc', accounts: [1, 2] },
  accounts: [
    { id: 1, email: 'wecc@sweden.se' },
    { id: 2, email: 'wecc@greece.gr' }
  ]
}

现在在我的 JSONAPISerializer 中,我如何从旧的 json 中获取 JSONAPI 格式。我正在使用旧格式。但我希望该格式自动转换为 JSONAPI。像下面一个

{
  data: { 
    id: '1', 
    type: 'user', 
    attributes: {
      name: 'wecc'
    }, 
    relationships: {
      accounts: {
        data: [
          { id: '1', type: 'account' },
          { id: '2', type: 'account' }
        ]
      }
    }
  },
  included: [{ 
    id: '1',
    type: 'account',
    attributes: {
      email: 'wecc@sweden.se'
    }
  }, {
    id: '2',
    type: 'account',
    attributes: {
      email: 'wecc@greece.gr'
    }
  }]
}

我从 ember 中找到了一些帮助。

http://emberjs.com/blog/2015/06/18/ember-data-1-13-released.html#toc_internal-format-change-to-json-api 他们建议使用 normalizeResponse

【问题讨论】:

    标签: ember.js ember-data jsonserializer json-api


    【解决方案1】:

    它不会自动发生。如果您还可以控制 REST API,我建议您对服务器本身的数据进行规范化。否则,正如您所建议的,它必须在 normalizeResponse 内发生,但要编写自定义代码以匹配您的数据格式。

    另一个解决方案是暂时继续使用RESTSerializer,并等到 JSON API 规范变得更流行一点。未来几个月可能会发布适用于大多数服务器框架的工具,以提供标准 JSON API 负载。

    【讨论】:

    • 感谢@PedroCheckos ...我找到了一些方法来做到这一点。您必须将您的 Ember RESTAdapter 更改为 JSONAPIAdapter 并在您的 ember-data 库中(我使用 version1.13.3 ) 将 isNewSerializerAPI: false 更改为 true...并最终返回名为 _normalizeResponse 的库中的一种方法,控制台返回数据,如 .... @ 987654325@ 因此,每当您的适配器请求请求时,它都会在浏览器控制台中向您显示 JSONAPI 数据格式...
    猜你喜欢
    • 1970-01-01
    • 2015-11-26
    • 2016-02-05
    • 1970-01-01
    • 2015-12-11
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 2012-11-03
    相关资源
    最近更新 更多