Ember数据JSONAPI复杂属性数据

时间:2015-08-21 13:57:06

标签: ember.js ember-data json-api

我有一个数据结构,如下所示从服务器返回,我正在写一些过滤功能。每个过滤器组都有许多过滤器。

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在这样的结构上的工作方式?

1 个答案:

答案 0 :(得分:3)

在JSON API中,虽然您可以在属性中嵌入数据,但您不能/不应该嵌入完整的资源对象(即具有自己的typerelationships等的对象)。我猜这就是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
    }
  }]
}

然后,您可能必须使用Ember Data中embedded标志以外的其他内容来获取所包含的资源 - 我不确定。但这绝对是从JSON API方面做到这一点的方法。