【问题标题】:Properly formatting Ember custom serializer response for sideloaded JSON API data为侧载 JSON API 数据正确格式化 Ember 自定义序列化程序响应
【发布时间】:2017-12-13 21:36:44
【问题描述】:

我想尝试为我的 Ember 应用提供 sideloaded data。我有一个 city 模型 hasMany fireStations。我将我的 hasMany 关系更改为有一个 { async: false } 选项以配合侧加载,因为数据将不再异步加载。

我使用自定义序列化程序,并记录来自normalize() 的响应。我的数据看起来像这样。

{
  "data": {
    "id": "3",
    "type": "city",
    "attributes": {
      "name": "Anytown USA"
    },
    "relationships": {
      "fireStations": {
        "data": [
          {
            "id": "17",
            "type": "fire-station"
          },
          {
            "id": "18",
            "type": "fire-station"
          }
        ]
      }
    }
  },
  "included": [
    {
      "id": "17",
      "type": "fire-station",
      "attributes": {
        "name": "North Side Fire Station"
      },
      "relationships": {}
    },
    {
      "id": "18",
      "type": "fire-station",
      "attributes": {
        "name": "East Side Fire Station"
      },
      "relationships": {}
    }
  ]
}

认为我的侧载数据格式正确。它似乎与example in the guides 匹配。 included 数组中填充了我所有的侧载数据,并且似乎都根据需要进行了格式化。

但是,我在我的应用程序中遇到了这个错误。

断言失败:您在 id 为 3 的“城市”上查找了“fireStations”关系,但未加载一些相关记录。要么确保它们都与父记录一起加载,要么指定关系是异步的 ('DS.hasMany({ async: true })')

根据 JSON API 规范,我使用的格式似乎对于加载 compound documents 是正确的。

在复合文档中,所有包含的资源必须表示为顶级 included 成员中的资源对象数组。

而且似乎links 是可选的,所以我将它们排除在外应该没问题。

每个资源对象中的可选links 成员包含与资源相关的链接。

我无法弄清楚这里的问题是什么。我在本地分叉了ember-data,我确实看到了this assertion is triggered

如果我手动遍历has-many.js 中的manyArray,我会看到每条记录都标记为isEmpty,即true

为什么has-many 记录返回isEmpty === true?我可能做错了什么导致侧载无法正常工作?

【问题讨论】:

    标签: ember.js ember-data


    【解决方案1】:

    我能够创建一个Ember Twiddle 来查找、重新创建和解决我的问题。

    简短的回答是我的自定义序列化程序在规范化期间丢弃了included。我混淆了normalize()normalizeResponse() 之间的职责,并从错误的方法返回included

    我试图在normalize() 的哈希上设置included 属性(这就是您在我上面的问题中看到的)。这种做法是完全错误的。

    included 应该出现在来自normalizeResponse() 的响应中,如果需要的话。所以我(多余地)在response() 中有included,但在normalizeResponse() 中实际需要的地方没有它。

    normalizeResponse (store, primaryModelClass, payload, id, requestType) {
      let { data, included } = this._super(...arguments);
    
      // Do some manipulation on `data` or `included`.
    
      // Oh no! We lost the `included` array!
      // This will cause an error
      // because we are saying the
      // relationship is not async, but we're
      // dropping the `included` data!
      return { data };
    
      // This is what we want, for this contrived example.
      // return { data, included };
    }
    

    如果需要返回 included 数组,则应从 normalizeResponse() 返回,而不是从 normalize() 返回。对于任何查看JSONAPISerializer in json-api.js 源代码的人来说,这应该是显而易见的,但我不知何故迷路了。

    the JSONAPISerializer implementation for ember-data 2.17.0 中,您可以看到includednormalizeResponse() 调用的方法中设置在documentHash 上。

    我的序列化程序做了很多自定义操作,因为我的 API 非常不标准,而且我误解了这里的一些细节。此外,尽管有文档,但我认为在 each of the various normalize... API hooks 中应该和不应该做什么有点难以理解。不过,我想如果我花更多时间阅读它们,我可能会明白这一点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-12-27
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-14
      • 1970-01-01
      相关资源
      最近更新 更多