2012-12-01 45 views
1

我剛剛偶然發現了emberJS,並認爲值得爲我的下一個Web應用程序嘗試它。在Ember把手中使用嵌套每個塊時的上下文綁定

我實際打算做的是以二維方式顯示對象列表(atm可以是表格)。

我到目前爲止有:

<script type="text/x-handlebars"> 
<table width="100%" style="text-align:left"> 
    <tr> 
    <th> 
    </th> 
    {{#each App.MyController.getColumnValues}} 
     <th>{{this}}</th> 
    {{/each}} 
    </tr> 
    {{#each App.MyController.getRowValues}} 
    <tr> 
     <th>{{this}}</th> 
     {{#each App.MyController.getColumnValues}} 
     {{view App.CountView rowBinding="this" columnBinding="../this"}} 
     {{/each}} 
    </tr> 
    {{/each}} 
</table> 
</script> 

,爲countView:

<script type="text/x-handlebars" data-template-name="countView"> 
    <td>{{view.row}} - {{view.column}}</td> 
</script> 

正如你可以看到,我想在每個單元具有當前行和列的值的表。 一切正常,除了columnBinding。正如我在Handlebars的頁面上所看到的{{../ this}}是解決父模板範圍的方法。在兩個花括號中(沒有爲它創建額外的視圖,這工作很好) 但我後來需要調用一個函數,將列和行值傳遞給它,並認爲(使它清晰),視圖將會很好?點

任何想法如何訪問父模板範圍,並把它傳遞給countView

回答

2

爲了避免您可以隨時使用變量{{each}}塊在這種情況下的混亂,你可以試試這個方法:

{{#each row in App.MyController.getRowValues}} 
    <tr> 
    <th>{{row}}</th> 
    {{#each column in App.MyController.getColumnValues}} 
     {{view App.CountView rowBinding="row" columnBinding="column"}} 
    {{/each}} 
    </tr> 
{{/each}} 

讓我知道這是否有幫助...

+0

這個工程。非常感謝。在閱讀Ember指南時,沒有遇到在每個循環中命名的項目。你可能知道一些教程或更好的說明嗎? Ember API不是很清晰。 – Woyzeck

+1

這可在emberjs網站http://emberjs.com/api/classes/Ember.Handlebars.helpers.html#method_each裏面的API部分而不是文檔 –

+0

上獲得,您也可以訪問http://handlebarsjs.com/獲取更多信息詳細的信息,其中包括寫你自己的幫手 –