2012-07-29 52 views
1

具有應表示一個表,具有columnsrows的圖:Ember.js /把手嵌套每個迭代

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

- 分別模板:

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

...其可以是發現on jsfiddle以及:jsfiddle.net/nWnf2

我顯然不能迭代列嵌套在行中。根本沒有觸發{{#each ../columns}}。這是爲什麼?什麼是更好的方法?

回答

4

Ember並不真正支持Handlebars中的文件路徑標識符。但是,您可以通過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/

看你的代碼片段,我建議你看看使用的{{#collection}}代替{{#each}}爲好,也不會緩解這個問題,但它使你的代碼更清潔(IMO)。