2015-08-21 47 views
1

我有一个数据结构如下从服务器返回我正在写的一些过滤功能。每个过滤器组都有多个过滤器。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在这样的结构上的工作方式吗?

回答

3

在JSON API中,虽然可以在属性中嵌入数据,但不能/不应该嵌入完整的资源对象(即具有自己的type,relationships等的对象)。我猜这就是绊倒Ember Data的原因。

相反,JSON API会要求您将这些嵌入式资源置于included集合中(请参见下文)。这允许主数据中的多个资源引用相同的included资源,而不需要在有效负载中多次包括该资源。因此,服务器的响应应该是这样的:

{ 
    "data": [{ 
    "type": "filter-group", 
    "id": "556d7f5fa1f9de08500ef4e8_1", 
    "attributes": { 
     "name": "Colour", 
     "created-date": "0001-01-01T00:00:00Z", 
     "active": true 
    }, 
    "relationships": { 
     "filters": { 
     "data": [ 
      {"type": "filters", "id": "556d7f5fa1f9de08500ef4e8_1_1"}, 
      {"type": "filters", "id": "556d7f5fa1f9de08500ef4e8_1_2"}, 
      {"type": "filters", "id": "556d7f5fa1f9de08500ef4e8_1_3"} 
     ] 
     } 
    } 
    }], 
    "included": [{ 
    "type": "filters", 
    "id": "556d7f5fa1f9de08500ef4e8_1_1", 
    "attributes": { 
     "name": "Red", 
     "created-date": "0001-01-01T00:00:00Z", 
     "active": true 
    } 
    }, { 
    "type": "filters", 
    "id": "556d7f5fa1f9de08500ef4e8_1_2", 
    "attributes": { 
     "name": "Blue", 
     "created-date": "0001-01-01T00:00:00Z", 
     "active": true 
    } 
    }, { 
    "type": "filters", 
    "id": "556d7f5fa1f9de08500ef4e8_1_3", 
    "attributes": { 
     "name": "Green", 
     "created-date": "0001-01-01T00:00:00Z", 
     "active": true 
    } 
    }] 
} 

然后,你可能必须使用比在灰烬数据embedded标志以外的东西把它捡起包括资源 - 我不知道。但是这绝对是通过JSON API来实现的。

+0

刚刚进入办公室,并给了这个尝试。你有它几乎正确,但“包含”的对象需要遵循相同的JSONAPI结构作为常规数据,即。用“id”,“type”,然后是“attributes”对象。给了我一个很好的开始让它工作的地方。我编辑了你的答案,以包含这些变化:) –

+0

哦,你不需要使用{embedded:'always'}。 –

+0

D'oh,当然'included'也需要'属性'。赶上@MalabarFront,我很高兴它现在几乎可以工作:) – Ethan