2017-10-28 67 views
1

我在另一個幫助器中使用了一個幫助器。我試圖傳遞一個值'post_id',我從'show_post'helper動態獲取。我想傳遞它,然後在返回一組結果返回給helper的查詢中使用它。出於某種原因,應用程序崩潰。有人可以通過這個指導我。在幫助函數中傳遞變量

{{#each show_post}} 
{{posttext}} 

{{postedBy}} 

    {{#each show_comment({{post_id}}) }} 
    //<--- i am passing the value to helper like this. 
    <span> <p>{{postedBy}}: <h5>{{commenttext}}</h5>{{createdAt}}</p></span>      
    {{/each}} 

    {{/each}} 
    Template.display_block.helpers({ 
    show_post(){ 
    return PostComment.find({parent: 0},{sort: {createdAt: -1}}); 
    } 
    }); 

Template.display_block.helpers({ 
show_comment(e){ 
var t1 = e; 
var now = PostComment.find({post_id:{$regex:new RegExp(’^’ + t1)}}); 
return now; 
} 
}); 

第一輔助產生一個陣列(蒙戈Db的結果).I要在下一助手使用此數組的元素(即是動態的)。所以我想使用一個變量,它可以保存第一個幫助器的數組元素的值,然後將它傳遞給下一個幫助器。在第二個幫助程序中,我想要傳遞此變量並使用此變量對Mongo查詢中的結果進行排序。我是新來的,所以我無法理解這個實例

回答

1

首先,你不需要用雙花括號或者parens來包裝helper參數。

{{#each show_comment post_id}} 

將做你所需要的。

你也讓生活變得有點複雜。您可以通過代碼中的this使用當前的數據上下文。還不清楚你爲什麼使用正則表達式,除非你將這些東西連接到_id

這可以讓你下簡化爲:

HTML:

{{#each show_post}} 
    {{posttext}} 
    {{postedBy}} 
    {{#each show_comment}} 
    <span><p>{{postedBy}}: <h5>{{commenttext}}</h5>{{createdAt}}</p></span>      
    {{/each}} 
{{/each}} 

JS:

Template.display_block.helpers({ 
    show_comment(){ 
    return PostComment.find({post_id: this._id); 
    } 
});