我使用Ember 2.4.1并尝试使用自引用模型构建分层树。
这是我的Ember模型
/categories
带有嵌入数据的路由{"categories":[
{
"id":4,
"name":"Root element w/o children",
"type":"Category",
"children":[]
},
{
"id":5,
"name":"Root element with a child",
"type":"Category",
"children":[
{
"id":6,
"name":"A child",
"type":"Category",
"children":[]
}
]
}
]}
中的服务器响应(我在Ember端使用RESTAdapter):
Assertion Failed: You looked up the 'children' relationship on a 'category' with id 5 but some of the associated records were not loaded. Either make sure they are all loaded together with the parent record, or specify that the relationship is async (DS.hasMany({ async: true }))
正如您所看到的,带有ID 5的类别具有ID为6的子项并且已嵌入。但是当Ember加载页面时出现错误:{categories: [{id:4, …}, {id:5, …}, {id:6, …}]}
如果我在JSON-root中包含id为6的类别,如async: true
错误将消失,但我不知道为什么我需要在这种情况下嵌入。可能我不明白嵌入在Ember中是如何工作的。
所以我想问一下如何使用嵌入式响应正确使用Ember Data,而不使用// app/serializers/category.js
import DS from 'ember-data';
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
children: { embedded: 'always' }
}
});
重复它。
UPD @TheCompiler在评论中回答了我的问题。使用mixin添加序列化程序可以解决我的问题:
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"1", GitVersion:"v1.1.2", GitCommit:"3085895b8a70a3d985e9320a098e74f545546171", GitTreeState:"clean"}
Server Version: version.Info{Major:"1", Minor:"1", GitVersion:"v1.1.2", GitCommit:"3085895b8a70a3d985e9320a098e74f545546171", GitTreeState:"clean"}
答案 0 :(得分:0)
无需复制记录。 Rest Adapter对嵌套的JSON对象不起作用。尝试格式化这样的数据:
{"categories":[
{
"id":4,
"name":"Root element w/o children",
"type":"Category",
"children":[]
},
{
"id":5,
"name":"Root element with a child",
"type":"Category",
"children":[6]
},
{
"id":6,
"name":"A child",
"type":"Category",
"children":[]
}
]}