使用include的嵌套关系

时间:2017-09-04 22:42:18

标签: ember.js ember-data

我正在写一个ember应用程序来显示餐馆的菜单。数据被组织成一个餐厅,有许多菜单部分,这些菜单部分本身有许多项目。

在我的路线中,我使用findRecord加载顶级餐厅记录,其中包含设置以包括部分和项目。服务器生成以下JSONAPI:

{
    "links": {
        "up": "http://localhost/api/restaurants",
        "self": "http://localhost/api/restaurants/0b27fd96-90e8-11e7-81c2-08002787e7fb"
    },
    "data": {
        "type": "restaurant",
        "id": "0b27fd96-90e8-11e7-81c2-08002787e7fb",
        "attributes": {
            "title": "Made-Up Foodery",
            "contact-number": "+1-555-1234",
            "restaurant-address": "Made-up, address F4K 3ED",
            "minimum-order": 500,
            "delivery-cost": 250
        },
        "relationships": {
            "menu-sections": {
                "links": {
                    "related": "http://localhost/restaurants/0b27fd96-90e8-11e7-81c2-08002787e7fb/menu-sections"
                },
                "data": [
                    {
                        "type": "menu-section",
                        "id": "23760716-1b75-4880-bae4-a6daaef3fc98"
                    }
                ]
            }
        }
    },
    "included": [
        {
            "type": "menu-section",
            "id": "23760716-1b75-4880-bae4-a6daaef3fc98",
            "attributes": {
                "title": "Test Section",
                "description": "This is a test section."
            },
            "relationships": {
                "menu-items": {
                    "links": {
                        "related": "http://localhost/menu-sections/23760716-1b75-4880-bae4-a6daaef3fc98/menu-items"
                    },
                    "data": [
                        {
                            "type": "menu-item",
                            "id": "a62e2ae0-326b-4838-b5ba-ffa102fcafb1"
                        }
                    ]
                }
            },
            "links": {
                "self": "http://localhost/menu-sections/23760716-1b75-4880-bae4-a6daaef3fc98"
            }
        },
        {
            "type": "menu-item",
            "id": "a62e2ae0-326b-4838-b5ba-ffa102fcafb1",
            "attributes": {
                "food-name": "Test Food",
                "price": 350,
                "is-hot": true,
                "is-vegetarian": false,
                "contains-nuts": false
            },
            "links": {
                "self": "http://localhost/menu-items/a62e2ae0-326b-4838-b5ba-ffa102fcafb1"
            }
        }
    ]
}

在ember中,我定义了具有适当关系的模型:

// restaurant.js
export default DS.Model.extend({
    title: DS.attr(),
    contactNumber: DS.attr(),
    restaurantAddress: DS.attr(),
    deliveryCost: DS.attr(),
    minimumOrder: DS.attr(),
    menuSections: DS.hasMany('menu-section')
});

// menu-section.js
export default DS.Model.extend({
    title: DS.attr(),
    description: DS.attr(),
    restaurantDetails: DS.belongsTo('restaurant'),
    items: DS.hasMany('menu-item')
});

// menu-item.js
export default DS.Model.extend({
    foodName: DS.attr(),
    price: DS.attr(),
    isVegetarian: DS.attr(),
    isHot: DS.attr(),
    containsNuts: DS.attr(),
    menuSection: DS.belongsTo('menu-section')
});

在我的路线中,我做了:

let restaurant = this.get('store').findRecord('restaurant', '0b27fd96-90e8-11e7-81c2-08002787e7fb', {include: 'menuSections,menuSections.items'});

然而,据我所知,在加载菜单部分对象时,菜单项不是。

据我所知,服务器发出的JSONAPI是正确的。

我做错了什么,或者这是不是由ember-data支持?

1 个答案:

答案 0 :(得分:0)

所以问题似乎是:

"relationships": {
                "menu-items": {

我错误地认为我应该在这里使用复数类型,但它需要匹配菜单部分模型中的名称items但需要menuItems