2017-04-04 32 views
1

我正在使用RESTAdapter並試圖找出如何訪問sideloaded數據。
有效載荷的樣品是:Ember:使用ember-data訪問setupController中的sideloaded數據

{ 
    "category": { 
     "categoryName": "test category", 
     "id": 6, 
     "products": [ 
      4419, 
      502, 
      3992 
     ] 
    }, 
    "products": [{ 
     "description": "Whatevs", 
     "id": 4419, 
     "name": "Product 1", 
     "price": 114.95, 
     "skuid": "S21046" 
    }, { 
     "description": "Whatevs", 
     "id": 502, 
     "name": "Product 2", 
     "price": 114.95, 
     "skuid": "SOLS2594" 
    }, { 
     "description": "Whatevs", 
     "id": 3992, 
     "name": "Product 3", 
     "price": 114.95, 
     "skuid": "S21015" 
    }] 
} 

我可以看到在餘燼檢查員「類別」和「產品」的數據模型(和數據),所以我知道他們正在被加載。

我甚至可以訪問模板model.products中的產品。但我不能在路線的setupController中訪問model.products。我得到的錯誤是:

TypeError: Cannot read property '_relationships' of undefined 

這實在讓我很困惑!我的路線model鉤:

model(params) { 
    return this.get('store').queryRecord('category', { 
     id: params.id 
    }) 
} 

setupController鉤(導致該錯誤)是:

setupController(controller, model) { 
    controller.set('results', model.products); 
} 

的 '類別' 的模式:

export default DS.Model.extend({ 
    products: hasMany('product'), 
    categoryName: attr('string') 
}); 

的 '產品' 的模式:

export default DS.Model.extend({ 

    name: attr('string'), 
    skuid: attr('string'), 
    price: attr('number'), 
    description: attr('string') 

}); 

我的模板(工作,如果我從路徑刪除「setupController」鉤):

{{#each model.products as |product|}} 
{{product.name}} {{product.skuid}}<br /> 
{{/each}} 

我希望能夠從該路由的setupController訪問model.products這樣我就可以把它叫做別的東西。任何幫助讚賞。

回答

2

關係返回承諾。所以要得到結果你需要使用then。但在模板中訪問它會工作,因爲默認情況下模板是承諾意識。

setupController(controller, model) { 
    //controller.set('results', model.products); 
    model.get('products').then((result) => { 
    controller.set('results',result); 
    }); 
} 

請給予閱讀relationships as promises指南。

相關問題