Ember.js / Handlebars nested each iterations

风流意气都作罢 提交于 2019-12-11 08:36:25

问题


Having a view that should represent a table, having columns and rows:

App.ListView = Ember.View.extend({
  templateName: 'list',
  columns: ['t1', 't2', 't3'],
  rows: [
    { t1: 'one', t2: 'two', t3: 'three' },
    { t1: 'two', t2: 'one', t3: 'seven' }
  ]
});

- respectively a template:

<table>
<thead>
  <tr>
  {{#each columns}}
    <th>{{this}}</th>
  {{/each}}
  </tr>
</thead>
<tbody>
{{#each rows}}
  <tr>
  {{#each ../columns}}
    <td>does nothing: {{this}}</td>
  {{/each}}
  </tr>
{{/each}}
<tbody>
<table>

... which can be found on jsfiddle as well: jsfiddle.net/nWnf2

I obviously can not iterate over columns when nested in rows. {{#each ../columns}} simply is not triggered at all. Why is that? What is a better approach?


回答1:


Ember doesn't really support the file path identifiers from Handlebars. You can however access via view.columns.

<table>
  <thead>
    <tr>
    {{#each columns}}
      <th>{{this}}</th>
    {{/each}}
    </tr>
  </thead>
  <tbody>
  {{#each rows}}
    <tr>
    {{#each view.columns}}
      <td>does nothing: {{this}}</td>
    {{/each}}
    </tr>
  {{/each}}
  <tbody>
<table>

http://jsfiddle.net/L7MAp/

Looking at your code snippet, I suggest you look at using {{#collection}} instead of {{#each}} as well, it won't alleviate that issue but it makes your code cleaner (IMO).



来源:https://stackoverflow.com/questions/11713833/ember-js-handlebars-nested-each-iterations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!