I'm writing an ember application to show a restaurant's menu. The data being organised as a restaurant having many menu sections which themselves have many items.
In my route I'm using findRecord to load the top-level restaurant record with include set to include the sections and items. The server produces the following 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"
}
}
]
}
In ember I define models with appropriate relationships as:
// 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')
});
In my route I do:
let restaurant = this.get('store').findRecord('restaurant', '0b27fd96-90e8-11e7-81c2-08002787e7fb', {include: 'menuSections,menuSections.items'});
However as far as I can tell whilst the menu-section objects are loaded, the menu-items are not.
As far as I can tell from my research the JSONAPI emitted by the server is correct.
Am I doing something wrong or is this simply not supported by ember-data?
Aucun commentaire:
Enregistrer un commentaire