d2kagw
d2kagw

Reputation: 807

Accessing Values for Key in Handlebars & Ember

Is it possible to access properties in a value-for-key style in Handlebars?

I have a CollectionView which uses an ArrayController full of models. The CollectionView has a property called 'columns' that define table column configurations for rendering.

Ideally I'd be able to loop through each column (see example below) ensuring that only the columns we want rendered are rendered (and later, formatting and other attributes are applied)

<tr>
  {{#each column in view.controller.columns}}
    <td>
      {{ view.content.[column.name] }}
    </td>
  {{/each}}
</tr>

This doesn't work, it just returns no content.

I've also tried these other styles to see if they'd work:

<tr>
  {{#each column in view.controller.columns}}
    <td>
      {{ view.content.name }}
      {{ view.content.[column.name] }}
      {{valForKey view.content column.name }}
    </td>
  {{/each}}
</tr>

The valForKey helper is one I wrote (source here), which does display the correct value but doesn't bind, so the value isn't updated when the property changes.

What's the best way to handle this use case in Ember?

Thanks

Upvotes: 5

Views: 1222

Answers (2)

Karol Szambelańczyk
Karol Szambelańczyk

Reputation: 141

Right now Ember has get helper included:

{{get object key}}

Upvotes: 1

Kalman
Kalman

Reputation: 8131

You can create a bound helper to display the value of the column

Ember.Handlebars.registerBoundHelper('dd', function(rowData, col) {
  return rowData[col];
});

See the following SO answer for more details

https://stackoverflow.com/a/27477602/908842

Upvotes: 0

Related Questions