2015-02-24 17 views
0

,我有以下數據對象:把手JS - 不能從循環訪問父對象的屬性/部分

enter image description here

而且有了這個,我現在用的是下面的循環沒有問題

Templates.Links = [ 
"<ul>", 
"{{#each data}}", 
    "<li>", 
    "<div class='flex-item-button'>", 
     "<div class='info noicon'>", 
      "<span class='name'>{{{URL}}}</span>", 
      "<span class='meta'>{{URL}}</span>",         
     "</div>", 
     "<div>",     
     "<span class='expand expand-item-details'><i class='fa fa-sort-down'></i></span>", 
     "</div>", 
     "<div class='additional-info'>", 

     "</div>", 
    "</div>",  
    "</li>", 
"{{/each}}", 
"</ul>",  
].join("\n"); 

這是沒有問題,並與每個數據循環我可以訪問我的數據屬性。然而,我想要做的是從我的循環中訪問標題/ caml字段。我嘗試了以下,但沒有運氣。

{{./title}} 
{{../title}} 

我理想地想在每個循環中使用一個部分,然後從該部分訪問父節點,但似乎也沒有工作。

回答

3

這裏是解決方案:

{{#each data}} 
    .... 
    {{#with ../this}} 
    {{> yourPartial}} 
    {{/with}} 
    .... 
{{/each}} 

而在你的部分,簡單地說:

{{title}} 

編輯。 你應該實現自己的幫手,如:

Handlebars.registerHelper("withCurrentItem", function(context, options){ 
    var contextWithCurrentItem = context; 
    contextWithCurrentItem.currentItem = options.hash.currentItem; 
    return options.fn(contextWithCurrentItem); 
}); 

而且使用它像:

{{#withCurrentItem ../this currentItem=this}} 
    {{> yourPartial}} 
{{/withCurrentItem}} 
+0

我現在我要傳遞的數據對象,以及?所以我可以訪問父元素和子項目? – kalabo 2015-02-24 12:32:41

+0

我是這樣描述的,你的部分將被父對象訪問 '{data:[],title:'..',caml:''....} 或者你需要一些更具體的邏輯嗎?我這麼說,請描述一下。 – intale 2015-02-24 12:54:57

+0

基本上從我的部分我需要訪問數據循環(每個塊)中的當前項目和父對象屬性basetemplate /標題等。 – kalabo 2015-02-24 13:01:49