如何处理大型关系数据属性和复合文档?

时间:2015-11-13 13:58:43

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

如果一篇文章有​​多条评论(随着时间的推移,可以考虑数千条)。 data.relationships.comments应该有限制返回吗?

{
  "data": [
    {
      "type": "articles",
      "id": 1,
      "attributes": {
        "title": "Some title",
      },
      "relationships": {
        "comments": {
          "links": {
            "related": "https://www.foo.com/api/v1/articles/1/comments"
          },
          "data": [
            { "type": "comment", "id": "1" }
            ...
            { "type": "comment", "id": "2000" }
          ]
        }
      }
    }
  ],
  "included": [
    {
      "type": "comments",
      "id": 1,
      "attributes": {
        "body": "Lorem ipusm",
      }
    },
    .....
    {
      "type": "comments",
      "id": 2000,
      "attributes": {
        "body": "Lorem ipusm",
      }
    },
  ]
}

当你想到复合文件(http://jsonapi.org/format/#document-compound-documents)时,这开始感到忧虑。这意味着,included部分也会列出所有注释,使JSON有效负载非常大。

1 个答案:

答案 0 :(得分:1)

如果您想限制从长列表中一次获得的记录数,请使用分页(JSON API spec)。

我会将comments分别加载store.queryember docs),就像这样 -

store.query('comments', { author_id: <author_id>, page: 3 });

将返回相关的评论子集。

如果您最初不希望为每位作者发出两个请求,则可以在作者请求中包含第一个“页面”,就像您现在所做的那样。

您可能还想查看像Ember Infinity这样的插件(未经测试),它会提供无限滚动列表并自动发出分页请求。