简体   繁体   English

如何从每个循环访问Ember.js中的模型字段

[英]How to access field of model in Ember.js from each loop

I have the next route: 我有下一条路线:

Calendar.DateIndexRoute = Ember.Route.extend({
  model: function(data) {
    return {arr:getCalendar(data),
        activeYear: data.year,
        activeMonthNumber: data.month,
        activeDay: data.day};
  },
  setupController: function(controller, model) {
    controller.set('model', model.arr);
    controller.set('activeYear', model.activeYear);
    controller.set('activeMonthNumber', model.activeMonthNumber);
    controller.set('activeDay', model.activeDay);
  }
});

arr is array of arrays. arr是数组的数组。 And in index.html I do next: 然后在index.html执行以下操作:

{{#each model}}
    <tr>
        {{#each}}
          {{#view Calendar.DateIndexView}}
            {{#if isToday}}
              <td class="today">
                {{number}}
            {{else}}
              {{#if isSelected}}
                <td class="selected">
              {{else}}
                <td class="day">
              {{/if}}
              {{number}}
            {{/if}}
            </td>
          {{/view}}
        {{/each}}
    </tr>
{{/each}}

But I need to access the fields activeYear and activeMonth in each loop: 但是我需要在每个循环中访问activeYearactiveMonth字段:

{{#each model}}
    <tr>
        {{#each}}
        access fields activeYear and activeMonth here
        {{/each}}
    </tr>
{{/each}}

I tried to use {{with}} component, but it isn't working. 我尝试使用{{with}}组件,但无法正常工作。 Also, I tried use setupController, and it isn't helping. 另外,我尝试使用setupController,但没有帮助。

I solved this problem as follows: 我解决了这个问题,如下所示:

  1. removed function setupController from route 从路由中删除了功能setupController
  2. In html change code: 在html更改代码中:

Resulting code is 结果代码是

{{#each item in model.arr}}
    <tr>
        {{#each day in item}}
          {{#if day.isToday}}
            <td class="today" {{action changeDay model.activeYear model.activeMonthNumber day.number}}>
              {{day.number}}
          {{else}}
            {{#if day.isSelected}}
              <td class="selected" {{action changeDay model.activeYear model.activeMonthNumber day.number}}>
            {{else}}
              <td class="day" {{action changeDay model.activeYear model.activeMonthNumber day.number}}>
            {{/if}}
            {{day.number}}
          {{/if}}
          </td>
        {{/each}}
    </tr>
{{/each}}

Your if statements aren't necessary for choosing the classes 您的if语句对于选择类不是必需的

App.DayController = Em.ObjectController.extend({
  notToday = Ember.computed.not('isToday'),
  notSelected = Ember.computed.not('isSelected'),
  isSelectedClass = Ember.computed.and('isSelected', 'notToday'),
  isNotSelectedClass = Ember.computed.and('notSelected', 'notToday')
});

{{#each day in item itemController='day'}}
   <td {{bind-attr class="day.isToday:today day.isSelectedClass:selected day.isNotSelectedClass:day"}} {{action changeDay model.activeYear model.activeMonthNumber day.number}}>
     {{day.number}}
   </td>
{{/each}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM