Meteor:操纵模板数据
这是一个流星/火焰问题 我有一个名为Lists的Mongo集合,其中包含postid列表,我想用另一个名为Posts的集合中的数据来查询和填充它 模式Meteor:操纵模板数据,meteor,meteor-blaze,Meteor,Meteor Blaze,这是一个流星/火焰问题 我有一个名为Lists的Mongo集合,其中包含postid列表,我想用另一个名为Posts的集合中的数据来查询和填充它 模式 Posts: [{ _id: String, // post data, I want to access this from a repeated list }] Lists: [{ _id: String, posts: [String] // of ids }] 列表模板: {{#if posts}} /// ref
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}}
{{{#if posts}}///参考List.posts
{{{#每个帖子}
//这里我想查询相应的帖子数据
{{/每个}}
{{/if}
我不知道如何使用Meteor。模板助手?一个优雅的解决方案是使用流行的软件包:
Lists.helpers({
posts: function(){
return Posts.find({
_id: {
// I suggest renaming your Lists.posts field to Lists.postIds.
$in: this.postsIds
}
});
}
});
然后你必须确保相应的帖子与你的列表一起发布,并且你可以像在伪代码中一样使用
{{{each posts}}
。我会尝试一下,让你知道。谢谢,“优雅”是这个解决方案的正确词汇。似乎模板中的数据保持不变:{{{{each postIds}}{{this}}{{/each}}
生成ID列表。我想我也可以使用template.list\u posts.helpers({})
?使用集合帮助器的优点是将逻辑与模型联系起来(它被称为fat模型范例)因此这些帮助程序将在整个应用程序中都可用,而不是在特定的模板帮助程序中。