glagola
glagola

Reputation: 2242

How to get an access in EmberJS template to a value of the object property within key stored in other variable?

I want to iterate over object properties in the specific order. I've prepared jsbin example of my usecase (with Ember.js and Handlebars). I can't find a way to make handlebars analog of

var months = ['2014-01-01', '2014-02-01', '2014-03-01'];

var datesByMonth = {
  '2014-03-01' : [ // march
    { date: '2014-03-10' },
    { date: '2014-03-20' },
    { date: '2014-03-21' }
  ],
  '2014-01-01' : [ // january
    { date: '2014-01-10' },
    { date: '2014-01-20' },
    { date: '2014-01-21' }
  ],
  '2014-02-01' : [ // february
    { date: '2014-02-10' },
    { date: '2014-02-20' },
    { date: '2014-02-21' }
  ],
};

var monthsCount = months.length;

for(var i = 0; i < monthsCount; ++i) {
  var month = months[i];
  console.log(month);
  
  var dates = datesByMonth[month];
  var datesCount = dates.length;
  
  for(var j = 0; j < datesCount; ++j) {
    var obj = dates[j];
    
    console.log('   ', obj.date);
  }
}

So, The question is How to get an access to a value of the object property within key stored in other variable?

P.S. Versions: EmberJS v1.9.1, Handlebars v2.0.0

P.S.S Sorry for my English

Upvotes: 0

Views: 199

Answers (2)

glagola
glagola

Reputation: 2242

I've just noticed that EmberJS since v1.10.0 uses HTMLBars as Handlebars template compiler. HTMLBars compiler allow to use helpers as subexpressions (In Ember v1.9.1 & Handlebars v2.0.0 the helper outputs his result directly to html, not to parent helper), so I've created handlebars analog of lookup helper:

Ember.Handlebars.registerBoundHelper('lookup', function(obj, key) {
  return obj && obj[key];
});

The helper allows to write such template code:

<ul>
  {{#each month in monthSorting}}
    <li>
      <h2>{{month}}</h2>
      <ul>
        {{#each obj in (lookup model month)}}
          <li>{{obj.date}}</li>
        {{/each}}
      </ul>
    </li>
  {{/each}}
</ul>

The helper solves the problem of accessing an inner property of an object by using a dynamic key name.

Upvotes: 1

Ramy Ben Aroya
Ramy Ben Aroya

Reputation: 2423

You can't access an inner property of an object just by using a dynamic key name. What you can do is manipulate your computed property months to return an array of objects which contains the month key and the dates, together.
Here is a working JS Bin example

App.IndexRoute = Ember.Route.extend({
  model: function() {
    return Ember.Object.create({
      '2014-03-01': [ // march
        {
          date: '2014-03-10'
        }, {
          date: '2014-03-20'
        }, {
          date: '2014-03-21'
        }
      ],
      '2014-01-01': [ // january
        {
          date: '2014-01-10'
        }, {
          date: '2014-01-20'
        }, {
          date: '2014-01-21'
        }
      ],
      '2014-02-01': [ // february
        {
          date: '2014-02-10'
        }, {
          date: '2014-02-20'
        }, {
          date: '2014-02-21'
        }
      ],
    });
  }
});
App.IndexController = Ember.Controller.extend({
  months: function() {
    var model = this.get('model'),
      months = Ember.keys(model);
    return Ember.A(months.map(function(month) {
      return Ember.Object.create({
        month: month,
        dates: model.get(month)
      });
    }));

  }.property('model'),

  monthSorting: Ember.computed.sort('months', function(x, y) {
    var xTime = new Date(x.get('month')).getTime(),
      yTime = new Date(y.get('month')).getTime();
    if (xTime > yTime) return 1;
    else if (xTime === yTime) return 0;
    else return -1;
  })
});
<script type="text/x-handlebars" data-template-name="index">
  <ul>
    {{#each month in monthSorting}}
    <li>
      <h2>{{month.month}}</h2>
      <ul>
        {{#each date in month.dates}}
        <li>{{date.date}}</li>
        {{/each}}
      </ul>
    </li>
    {{/each}}
  </ul>
</script>

Upvotes: 0

Related Questions