我有一个数据结构如下从服务器返回我正在写的一些过滤功能。每个过滤器组都有多个过滤器。Ember数据JSONAPI复杂属性数据
data: [
{
type: "filter-group",
id: "556d7f5fa1f9de08500ef4e8_1",
attributes: {
name: "Colour",
created-date: "0001-01-01T00:00:00Z",
active: true,
filters: [
{
id: "556d7f5fa1f9de08500ef4e8_1_1",
name: "Red",
created-date: "0001-01-01T00:00:00Z",
active: true
},
{
id: "556d7f5fa1f9de08500ef4e8_1_2",
name: "Blue",
created-date: "0001-01-01T00:00:00Z",
active: true
},
{
id: "556d7f5fa1f9de08500ef4e8_1_3",
name: "Green",
created-date: "0001-01-01T00:00:00Z",
active: true
}
]
}
}
]
而且我有模型设置为这样:
// models/filter-group.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
active: DS.attr('boolean'),
client: DS.belongsTo('client', { embedded: 'always' }),
filters: DS.hasMany('filter', { embedded: 'always' })
});
和:
// models/filter.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr('string'),
active: DS.attr('boolean'),
createdDate: DS.attr('date'),
filterGroup: DS.belongsTo('filter-group', { embedded: 'always' })
});
我是新来与JSONAPI工作,所以我不知道如果我的数据设置是正确的方式去做这件事。我通过过滤器组,然后在内部的每个,遍历其可用过滤器试图循环,使用下面的把手模板:
{{#each filterGroups as |filterGroup|}}
<h6>{{filterGroup.name}}</h6>
{{#each filterGroup.filters as |filter|}}
-- Filter output here --
{{/each}}
{{/each}}
但每次filterGroup.filters对象为空。我在这里做错了什么?我完全误解了JSONAPISerializer在这样的结构上的工作方式吗?
刚刚进入办公室,并给了这个尝试。你有它几乎正确,但“包含”的对象需要遵循相同的JSONAPI结构作为常规数据,即。用“id”,“type”,然后是“attributes”对象。给了我一个很好的开始让它工作的地方。我编辑了你的答案,以包含这些变化:) –
哦,你不需要使用{embedded:'always'}。 –
D'oh,当然'included'也需要'属性'。赶上@MalabarFront,我很高兴它现在几乎可以工作:) – Ethan