[英]EmberJS, How access in a child template to the model in the parent controller
我有這個router
:
// app/router.js
Router.map(function() {
this.route('battle', function(){
this.route('combats');
})
});
在戰斗路線中,我可以使用以下命令輕松訪問戰斗模型:
// app/routes/battle/combat.js
this.modelFor('battle');
但是,如果我也想在戰斗模板中訪問此模型,事情就會變得復雜起來:
// app/templates/battle/combats.hbs
<h1>Combats for Battle {{<how to access to the battle>.title}}</h1>
{{#each model as |combat|}}
{{combat.date}}
{{/each}}
我已經解決了從戰斗路線向戰斗控制器發送屬性的問題:
// app/routes/battle/combat.js
setupController: function(controller, model) {
controller.set('content', model);
controller.set('battle', this.modelFor('battle'));
}
但是我不知道這是否是正確的方法,在我看來,它看起來是間接的 ,就像您必須做很長的解決方法才能使此屬性在模板中可用。
這取決於您希望代碼的通用性。 對於您的特殊用例,可能需要在Fights.js的模型掛鈎中使用combats.js
如下所示:
model(){
return Ember.RSVP.hash({
combats: //here your combat.js model code,
battle: this.modelFor('battle');
})
}
然后,您可以刪除setupController函數並將模板重寫為:
<h1>Combats for Battle {{model.battle.title}}</h1>
{{#each model.combats as |combat|}}
{{combat.date}}
{{/each}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.