我正在使用的API端点返回其中包含多个嵌套关系的数据,我在normalizeResponse()
中使用DS.JSONAPISerializer
将其按到完全是JSON-API的内容兼容。
ember检查器显示所有数据都正确放置在各自的容器中。顶级模型与其hasMany
子级之间的链接确实有效,但嵌套模型之间的链接不起作用。我通过在检查器中导航到嵌套模型的子模型,点击它并观察它的内容来验证这一点。 property is null。
首先,看看我的模型是如何设置的:
// models/search.js
// i am able to browse from the search model to children with success
export default DS.Model.extend({
articles: DS.hasMany('article'),
});
// models/article.js
// i CANNOT browse from an article down to its digest in ember inspector
export default DS.Model.extend({
search: DS.belongsTo('search'),
type: DS.attr(),
created: DS.attr(),
updated: DS.attr(),
digest: DS.belongsTo('digest'),
});
// models/digest.js
export default DS.Model.extend({
title: DS.attr(),
desc: DS.attr(),
date: DS.attr(),
article: DS.belongsTo('article'),
});
现在,在normalizeResponse
内的函数完成后,我的修改后的JSON就行了。从normalizeResponse
返回此数据后,"摘要"父母之间的对象"关系"对象消失了。我的JSON有问题吗?我已尝试过如此多的排列,但没有成功,我很确定这符合Compound Documents的JSON-API规范。
{"data":{
"type":"searches",
"id":"17482738723",
"attributes":{
},
"relationships":{
"articles":{
"data":[
{
"type":"articles",
"id":"19988"
},
{
"type":"articles",
"id":"19989"
},
]
},
"digest":{
"data":[
{
"type":"digest",
"id":"19988_digest"
},
{
"type":"digest",
"id":"19989_digest"
},
]
}
}
},
"included":[
{
"id":"19988",
"type":"articles",
"attributes":{
"type": "internal",
"created":"2016-09-27T00:13:11.000Z",
"updated":"2016-09-27T00:13:11.000Z",
}
},
{
"id":"19988_digest",
"type":"digest",
"attributes":{
"title":null,
"desc":"four five six",
}
},
{
"id":"19989",
"type":"articles",
"attributes":{
"type": "internal",
"created":"2016-09-27T00:13:11.000Z",
"updated":"2016-09-27T00:13:11.000Z",
}
},
{
"id":"19989_digest",
"type":"digest",
"attributes":{
"title":"one two three",
"desc":null,
}
},
]
}
答案 0 :(得分:1)
您的回复表明以下关系模型:
self
所以你必须修复你的回复:
// models/search.js
export default DS.Model.extend({
articles: DS.hasMany('article'),
dignists: DS.hasMany('digest'),
});
// models/article.js
export default DS.Model.extend({
search: DS.belongsTo('search'),
});
// models/digest.js
export default DS.Model.extend({
search: DS.belongsTo('search'),
});
digest
关系
search
digest
关系
所以你会以这样的结局结束:
article
知道您也可以反过来执行此操作并在{
"data":{
"type":"searches",
"id":"17482738723",
"attributes":{
},
"relationships":{
"articles":{
"data":[
{
"type":"articles",
"id":"19988"
},
{
"type":"articles",
"id":"19989"
},
]
}
}
},
"included":[
{
"id":"19988",
"type":"articles",
"attributes":{
"type": "internal",
"created":"2016-09-27T00:13:11.000Z",
"updated":"2016-09-27T00:13:11.000Z",
},
"relationships":{
"digest": {
"data": {
"type":"digest",
"id":"19988_digest"
}
}
}
},
{
"id":"19988_digest",
"type":"digest",
"attributes":{
"title":null,
"desc":"four five six",
},
"relationships":{
"digest":{
"data": {
"type":"digest",
"id":"19989_digest"
}
}
}
},
{
"id":"19989",
"type":"articles",
"attributes":{
"type": "internal",
"created":"2016-09-27T00:13:11.000Z",
"updated":"2016-09-27T00:13:11.000Z",
}
},
{
"id":"19989_digest",
"type":"digest",
"attributes":{
"title":"one two three",
"desc":null,
}
},
]
}
上指定article
。 digest
会自动将所有内容保持同步。为了清晰起见,我个人更喜欢指定关系的两个方面。