【问题标题】:Meteor: Manipulate Template DataMeteor:操作模板数据
【发布时间】:2015-06-25 01:19:27
【问题描述】:

这是一个 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}}   /// 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}}

我不确定如何使用 Meteor 执行此操作。模板助手?

【问题讨论】:

    标签: meteor meteor-blaze


    【解决方案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}}

    【讨论】:

    • 我会试一试,让你知道。谢谢,“优雅”是这个解决方案的正确词。
    • 模板中的数据似乎保持不变:{{#each postIds}}{{this}}{{/each}} 生成一个 id 列表。
    • 我想我也可以使用Template.list_posts.helpers({}) ?
    • 使用集合助手的优势在于您将逻辑绑定到模型(称为胖模型范例),因此这些助手将在整个应用程序中可用,而不是在特定的模板助手中。
    猜你喜欢
    • 2016-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-11
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    相关资源
    最近更新 更多