2016-07-31 47 views
0

在動態細分模板中,如何使用路線顯示模型中的數據?Ember:如何訪問動態細分模板中的其他模型數據

所以,比如我有這三個途徑與phone_id動態段

Router.map(function() { 
    this.route('phones'); 
    this.route('phone', {path: 'phones/:phone_id'}); 
    this.route('numbers'); 
}); 

phones/:phone_id模板,我試圖顯示所有的數字模型。所以在phone.js路線中,我試圖返回數字模型並輸出它,但它什麼都沒顯示。

import Ember from 'ember'; 

export default Ember.Route.extend({ 

    numbers(){ 
    return this.get("store").findAll('number') 
    } 

}); 

我試過它也與params.phone_id作爲參數,但它沒有奏效。 (也沒有錯誤顯示)。

模板phone.hbs看起來像

<h5> Device Id: {{model.device_id}}</h5> 


{{#each numbers as |number|}} 
{{number.digits}} 
{{/each}} 

有趣的是model.device_id返回即使我甚至沒有將其設置爲返回,在phone.js路線正確的。但是我爲其實現的numbers的每個循環都不返回任何內容。

是否有解決方法來返回phone.hbs動態片段模板中的數字模型數據?

編輯

我達到我的動態段的方式是通過一個鏈接:

{{#each phones as |phone|}} 
    <li>{{#link-to 'phone' phone}} {{phone.id}}{{/link-to}}</li> 
{{/each}} 

回答

2

唯一對象從路線的設定爲控制器的範型鉤回來。

如果你想使用數字,因爲它是在模板中,然後將其作爲控制器中的計算屬性編寫。

numbers:Ember.computed(function(){ 
    return this.store.findAll('number'); 
}); 

,或者你可以設置模型這些屬性本身

你的路線,這樣模型鉤子將這個樣子

model:function(params){ 
return Ember.RSVP.hash({ 
    phone: this.store.findRecord('phone',params.phone_id), 
    numbers: this.store.findAll('number') 
}); 
} 

在此之後,你會在你的模型獲得兩個屬性

現在您的模板將如下所示

<h5> Device Id: {{model.phone.device_id}}</h5> 


{{#each model.numbers as |number|}} 
{{number.digits}} 
{{/each}} 
+0

這些不能在'phone.hbs'模板中使用,但可以在其他模板中使用。看起來像模型被覆蓋,因爲它是一個動態的細分市場 –

+0

告訴我一件事是你通過linkTo這個動態的細分市場路線?如果您使用linkto並將phoneModel作爲id參數傳入,那麼將跳過模型鉤子,並將控制器模型設置爲您從linkTo傳遞的模型。 –

+0

是的,我是,請看看我的編輯,讓我知道我可以通過id,而不是模型參數,因爲你提到。 –

相關問題