2015-06-25 74 views
1

这是一个流星/火焰问题。流星:操作模板数据

我有一个名为Lists的Mongo集合,它包含postIds列表,我想查询它并使用另一个名为Posts的集合中的数据填充。

架构

Posts: [{ 
    _id: String, 
    // post data, I want to access this from a repeated list 
}] 

Lists: [{ 
_id: String, 
posts: [String] // of ids 
}] 

列表模板:

{{#if posts}} /// referring to List.posts 
<div class="list-posts"> 
    <ul> 
    {{#each posts}} 

     // here I would like query for the corresponding Posts data 

    {{/each}} 
    </ul> 
</div> 
{{/if}} 

我不知道如何与流星做到这一点。模板助手?

回答

1

优雅的解决办法是使用流行的dburles:collection-helpers包:

Lists.helpers({ 
    posts: function(){ 
    return Posts.find({ 
     _id: { 
     // I suggest renaming your Lists.posts field to Lists.postIds. 
     $in: this.postsIds 
     } 
    }); 
    } 
}); 

然后,你必须确保相应的职位与你的列表一起发布,你就可以使用{{#each posts}}就像在你的伪代码中一样。

+0

我会试一试,让你知道。谢谢,'优雅'是这个解决方案的合适词。 – shmck

+0

看起来模板中的数据保持不变:'{{#each postIds}} {{this}} {{/ each}}'产生一个id列表。 – shmck

+0

我想我也可以使用'Template.list_posts.helpers({})'? – shmck