Question

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. And in index.html I do next:

{{#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:

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

I tried to use {{with}} component, but it isn't working. Also, I tried use setupController, and it isn't helping.

Était-ce utile?

La solution

I solved this problem as follows:

  1. removed function setupController from route
  2. In html change code:

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}}

Autres conseils

Your if statements aren't necessary for choosing the classes

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}}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top