Ember 与嵌入数据有很多关系的数据
Ember Data with hasMany relation on embedded data
我正在使用 Ember 2.4.1 并尝试使用自引用模型构建分层树。
这是我的 Ember 模型
// app/models/category.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
parent: DS.belongsTo('category', { inverse: 'children' }),
children: DS.hasMany('category', { embedded: 'always', async: false, inverse: 'parent' })
});
服务器在路由 /categories
中响应嵌入数据(我在 Ember 端使用 RESTAdapter):
{"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":[]
}
]
}
]}
如您所见,ID 为 5 的类别有一个 ID 为 6 的子项,并且它已嵌入。但是 Ember 加载页面时会出错: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 }))
如果我在 JSON-root 中包含 ID 为 6 的类别,例如 {categories: [{id:4, …}, {id:5, …}, {id:6, …}]}
错误将会消失,但我不知道为什么我需要在这种情况下嵌入。可能是我不明白 Ember.
中的嵌入是如何工作的
所以我想咨询一下如何使数据正常工作 Ember 具有嵌入式响应的数据而不复制它并且没有 async: true
。
UPD @TheCompiler 在评论中回答了我的问题。使用 mixin 添加序列化程序解决了我的问题:
// app/serializers/category.js
import DS from 'ember-data';
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
children: { embedded: 'always' }
}
});
无需复制记录。 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":[]
}
]}
我正在使用 Ember 2.4.1 并尝试使用自引用模型构建分层树。
这是我的 Ember 模型
// app/models/category.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
parent: DS.belongsTo('category', { inverse: 'children' }),
children: DS.hasMany('category', { embedded: 'always', async: false, inverse: 'parent' })
});
服务器在路由 /categories
中响应嵌入数据(我在 Ember 端使用 RESTAdapter):
{"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":[]
}
]
}
]}
如您所见,ID 为 5 的类别有一个 ID 为 6 的子项,并且它已嵌入。但是 Ember 加载页面时会出错: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 }))
如果我在 JSON-root 中包含 ID 为 6 的类别,例如 {categories: [{id:4, …}, {id:5, …}, {id:6, …}]}
错误将会消失,但我不知道为什么我需要在这种情况下嵌入。可能是我不明白 Ember.
所以我想咨询一下如何使数据正常工作 Ember 具有嵌入式响应的数据而不复制它并且没有 async: true
。
UPD @TheCompiler 在评论中回答了我的问题。使用 mixin 添加序列化程序解决了我的问题:
// app/serializers/category.js
import DS from 'ember-data';
export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
attrs: {
children: { embedded: 'always' }
}
});
无需复制记录。 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":[]
}
]}