如果一篇文章有多条评论(随着时间的推移,可以考虑数千条)。 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有效负载非常大。
答案 0 :(得分:1)
如果您想限制从长列表中一次获得的记录数,请使用分页(JSON API spec)。
我会将comments
分别加载store.query
(ember docs),就像这样 -
store.query('comments', { author_id: <author_id>, page: 3 });
将返回相关的评论子集。
如果您最初不希望为每位作者发出两个请求,则可以在作者请求中包含第一个“页面”,就像您现在所做的那样。
您可能还想查看像Ember Infinity这样的插件(未经测试),它会提供无限滚动列表并自动发出分页请求。