在Ember.ArrayController中访问单击的模型

时间:2014-05-26 12:06:09

标签: ember.js controller

我有一个小的Ember.js应用程序,它将当前月份显示为填充day模型的列表。想一个日历,您可以在其中点击特定日期以突出显示它们。

App.HolidaysNewRoute = Ember.Route.extend({
  actions: { 
    toggleSelected: function() {
      console.log(this.day);   # --------> day is undefined here
    }
});

App.HolidaysNewController = Ember.ArrayController.extend({
  days: function() {
    return [
      this.store.createRecord('day'),
      ...
    ];
  }.property()
});

<script type="text/x-handlebars" id="holidays/new">
   <ul>
     {{#each day in days}}
       <li {{action "toggleSelected"}}>{{day}}</li>
     {{/each}}
   </ul>
</script>

我如何知道在动作处理程序中点击了哪个day模型?我知道有一个currentModel,但这是一个ArrayController,有多个模型。

当我在模板中创建一个Ember组件并从循环中传递day模型时它可以工作,但这意味着我正在处理组件中的行为,我想我应该把它留在控制器。

谢谢!

1 个答案:

答案 0 :(得分:2)

更改你的toggleSelected函数以允许另一个参数

App.HolidaysNewRoute = Ember.Route.extend({
  actions: { 
    toggleSelected: function(obj) {
      console.log(obj);  
    }
});

然后在动作中发送对象

 {{#each day in days}}
   <li {{action "toggleSelected" day}}>{{day}}</li>
 {{/each}}