2016-05-29 30 views
0

我想知道您是否可以告訴我一些線索如何解決這個問題。我是JS(+ Meteor)的新手,所以對你來說可能會非常簡單。我正在與helpersblaze/spacebars進行渲染。流星/大火渲染:另一個{{each}}中的{{each}}不會返回正確的數據(數據上下文)

我有一個簡單的輔助:

Template.monitoring.helpers({ 

    'realtime': function(){ 
     return { 
      house: house.find({}), 
      neighbours: neighbours.find({}), 
     } 
    } // end of realtime 

}); // end of helpers 

在這一點上,一切都OK了。我能夠檢索我想要的數據。問題是我無法像我想要的那樣渲染它。在我的template下面。

<template name="monitoring"> 

    {{#each realtime.house}} 
    <div class="card red"> 
    SHOW STUFF ABOUT HOUSE 
    </div> 

    <div class="card blue"> 
     {{#each realtime.neighbours}} 
     SHOW STUFF ABOUT NEIGHBOURS OF THE SPECIFIC HOUSE 
     {{/each}} 
    </div> 

    {{/each}} -> end of each realtime.house 

</template> 

更確切地說,當我的模板呈現:

  • 數紅牌=房屋的數量=>完美
  • 每個紅牌包含一個房屋數據=>完美

  • 藍卡數量=房屋數量=>完美

  • 每塊藍卡包含的數據全部爲 =>錯誤。我想要的是關於特定房屋的鄰居的數據

你有沒有在正確的地方找到正確的東西? 非常感謝。

回答

0

您需要兩名助手:一名返回房屋,一名返回鄰居房屋作爲上下文。沒有模式,我無法給出準確的答案,但我們假設每個鄰居都有一個連接這兩個的houseId屬性。

Template.monitoring.helpers({ 
    houses: function() { 
    return house.find(); 
    }, 

    neighbors: function() { 
    // Note the context here is a house because it's 
    // called from within an each. 
    return neighbours.find({ houseId: this._id }); 
    }, 
}); 

而且你的模板會是這個樣子(假設鄰居應各自在一個藍色的卡):

<template name="monitoring"> 
    {{#each houses}} 
    <div class="card red"> 
     SHOW STUFF ABOUT HOUSE 
    </div> 
    {{#each neighbours}} 
     <div class="card blue"> 
     SHOW STUFF ABOUT NEIGHBOURS OF THE SPECIFIC HOUSE 
     </div> 
    {{/each}} 
    {{/each}} 
</template> 
+0

它回答完美!像魅力一樣工作! – nerotulip