Ember数据JSONAPI复杂属性数据 [英] Ember data JSONAPI complex attribute data

查看:165
本文介绍了Ember数据JSONAPI复杂属性数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a data structure as follows coming back from the server for some filter functionality I'm writing. Where each filter-group has many filters.

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' })
});

And:

// 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,所以我不知道我的数据设置是正确的做法我试图循环过滤器组,然后循环遍历其中的每个循环,使用以下句柄模板循环使用以下句柄模板:

I'm new to working with JSONAPI, so I'm not sure if my data setup is the right way of going about this. I'm trying to loop through the filter-groups and then inside each, loop through its available filters, using the following handlebars template:

{{#each filterGroups as |filterGroup|}}
    <h6>{{filterGroup.name}}</h6>

    {{#each filterGroup.filters as |filter|}}
        -- Filter output here --
    {{/each}}
{{/each}}

但是每个filterGroup.filters对象都是空的。我在这里做错了什么?我完全误解了JSONAPISerializer对这样的结构的工作方式吗?

But each filterGroup.filters object is empty. What am I doing wrong here? Am I completely misunderstanding the way the JSONAPISerializer works on structures like this?

推荐答案

在JSON API中,您可以在属性,你不能/不应该嵌入完整的资源对象(即具有自己的类型的对象关系等)。我猜这是Ember Data的绊脚石。

In JSON API, while you can embed data within an attribute, you can't/aren't supposed to embed full resource objects (i.e., objects with their own type, relationships, etc.). I'm guessing that's what's tripping up Ember Data.

相反,JSON API要求将这些嵌入式资源放在包含集合中(见下文)。这允许主数据中的多个资源引用包含的资源,而不需要在有效负载中多次包含该资源。所以服务器响应应该如下所示:

Instead, JSON API asks that you put these embedded resources in the included collection (see below). This allows multiple resources in the primary data to reference the same included resource, without that resource needing to be included multiple times in the payload. So the server response should look like this:

{
  "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
    }
  }]
}

然后,您可能需要使用嵌入 code> Ember Data中的标志,以获取包含的资源 - 我不知道。但这绝对是从JSON API方面做的。

Then, you may have to use something other than the embedded flag in Ember Data to get it to pick up the included resources—I'm not sure. But this is definitely the way to do it from the JSON API side.

这篇关于Ember数据JSONAPI复杂属性数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆