0

我有这个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'));
}

但我不知道这是否是正确的方法,在我看来它看起来太间接了,就像你必须做一个很长的解决方法才能使这个属性在模板中可用。

4

1 回答 1

1

这取决于您希望代码的通用性。对于您的特殊用例,可能适合在模型挂钩中使用 Ember.RSVP.hash,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}}
于 2015-12-21T12:52:20.470 回答