我们处理嵌套 API 路由的解决方案是使用请求的有效负载返回链接哈希。如果您可以更改 API 输出,那么这是目前的方法。例如,在请求工作区时,返回的 JSON 如下所示(请原谅转义字符):
{
"data":
{
"object":"workspace",
"id":"wrk_krVZWGaJ",
"organization_name":"Legros, Klein and Boehm",
"workspace_name":"Legros, Klein and Boehm",
"workspace_path":"legros, klein and boehm",
"status":true,
"credit_production":7,
"credit_revision":16,
"links":{
"projects":"\/v1\/workspaces\/wrk_krVZWGaJ\/projects",
"productions":"\/v1\/workspaces\/wrk_krVZWGaJ\/productions",
"subscription":"\/v1\/workspaces\/wrk_krVZWGaJ\/subscription",
"assets":"\/v1\/workspaces\/wrk_krVZWGaJ\/assets",
"descriptions":"\/v1\/workspaces\/wrk_krVZWGaJ\/descriptions",
"roles":"\/v1\/workspaces\/wrk_krVZWGaJ\/roles",
"registrations":"\/v1\/workspaces\/wrk_krVZWGaJ\/registrations",
"users":"\/v1\/workspaces\/wrk_krVZWGaJ\/users"
}
}
}
该模型将链接定义为 async hasMany 关系,即DS.attr('projects', {async: true})
. 从商店加载工作区时,不包括链接的项目。当您使用{{#each workspace.projects}}
或workspace.get('projects')
其他任何地方时,ember-data 将向 发出 GET 请求/v1/workspaces/wrk_krVZWGaJ/projects/
,而不是/projects/
. 尼托,嗯?
有关RESTAdapter findHasMany 方法的更多详细信息。