2017-09-04 94 views
0

我正在編寫一個應用程序來顯示餐廳的菜單。這些數據被組織成一家擁有許多菜單部分的餐廳,而這些菜單部分本身就有很多項目使用的嵌套關係包括

在我的路線中,我使用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" 
      } 
     } 
    ] 
} 

在餘燼我定義模型連同適當的關係:

// 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是正確的。

我做錯了什麼,或者這只是不支持的燼數據?

回答

0

如此看來,問題是:

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

我提出想我應該在這裏使用類型的複數的錯誤,但它需要匹配的名稱在菜單截面模型,是items,但需要是menuItems