如何显示“发布-复合集合"的子项?在模板视图上 [英] How to display children of "publish-composite collection" on Template View

查看:71
本文介绍了如何显示“发布-复合集合"的子项?在模板视图上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑在> https://github.com/englue/meteor-publish-复合

如何在模板视图上显示嵌套子级.我的意思是,在视图上显示帖子中的 前2条评论 .

How to display nested children on the template view. I mean, displaying top 2 comments on the post on the view.

我已经在Internet上进行了大量搜索,以查找模板视图"中的此子树显示内容,

I have searched a lot over internet for this children tree display on Template View, didn't find any.

代码

publishComposite('topTenPosts', {
    find() {
        // Find top ten highest scoring posts
        return Posts.find({}, { sort: { score: -1 }, limit: 10 });
    },
    children: [
        {
            find(post) {
                // Find post author. Even though we only want to return
                // one record here, we use "find" instead of "findOne"
                // since this function should return a cursor.
                return Meteor.users.find(
                    { _id: post.authorId },
                    { fields: { profile: 1 } });
            }
        },
        {
            find(post) {
                // Find top two comments on post
                return Comments.find(
                    { postId: post._id },
                    { sort: { score: -1 }, limit: 2 });
            },
            children: [
                {
                    find(comment, post) {
                        // Find user that authored comment.
                        return Meteor.users.find(
                            { _id: comment.authorId },
                            { fields: { profile: 1 } });
                    }
                }
            ]
        }
    ]
});

推荐答案

使用Blaze,它应该只是一组简单的模板,您可以在其中搜索助手中的相关评论和作者,并使用嵌套的循环.

Using Blaze it should just be a simple set of templates where you search for the related comments and authors in a helper, displaying the posts and comments with nested {{#each}} loops.

html:

<template name="posts">
{{#each posts}}
  Title: {{title}} posted on: {{date}} by: {{authorName}}
  {{#each comments}}
    {{> comment}}
  {{/each}}
{{/each}}
</template>

<template name="comment">
Post comment {{body}} by {{authorName}} on {{createdAt}}
</template>

现在为助手:

Template.posts.helpers({
  posts(){
    return Posts.find({}, { sort: { score: -1 }, limit: 10 });
  },
  comments(){
    return Comments.find({ postId: this._id },{ sort: { score: -1 }, limit: 2 });
  },
  authorName(){
    return Meteor.users.findOne(this.authorId).username;
});

Template.comment.helpers({
  authorName(){
    return Meteor.users.findOne(this.authorId).username;
  },
});

请注意在这些帮助器中使用this. this将是评估时数据 context 的值.在{{#each}}块内部,this代表当前文档的值,即带有键的对象.

Note the use of this in these helpers. this will be the value of the data context at the point of evaluation. Inside the {{#each}} blocks this takes on the value of the current document, i.e. an object with keys.

如果愿意,可以通过创建全局助手来保持authorName助手的干燥状态.

You can keep the authorName helpers DRY by creating a global helper if you like.

这篇关于如何显示“发布-复合集合"的子项?在模板视图上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆