【问题标题】:Meteor Reactive Data Query for Comments with Usernames and PicturesMeteor Reactive Data 查询用户名和图片评论
【发布时间】:2015-02-05 09:41:41
【问题描述】:

我正在尝试在一个巨大的应用程序中实现一个评论系统,并且总是遇到关于交叉反应和发布的问题。

具体问题:

当用户写评论时,我想显示用户的姓名和个人资料图片。 cmets 在一个集合中,名称和图片在另一个集合中。

当我订阅此页面上的每条评论以及 id 在此页面评论中服务器端的每个用户时,应用程序不会在添加新评论时更新客户端上可用的用户,因为“加入”在服务器上不活跃。

当我在客户端这样做时,我必须一直取消订阅和重新订阅,添加新评论并且负载变得更高。

在流星中实现这样一个系统的最佳实践是什么?我怎样才能在不过度发布的情况下解决这个问题?

【问题讨论】:

标签: mongodb join meteor


【解决方案1】:

由于还没有官方对加入的支持,在社区的所有解决方案中

我发现 https://github.com/englue/meteor-publish-composite 这个包非常有用,我正在我的应用程序中使用它。

这个例子完全符合你的要求https://github.com/englue/meteor-publish-composite#example-1-a-publication-that-takes-no-arguments

Meteor.publishComposite('topTenPosts', {
    find: function() {
        // Find top ten highest scoring posts
        return Posts.find({}, { sort: { score: -1 }, limit: 10 });
    },
    children: [
        {
            find: function(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 },
                    { limit: 1, fields: { profile: 1 } });
            }
        },
        {
            find: function(post) {
                // Find top two comments on post
                return Comments.find(
                    { postId: post._id },
                    { sort: { score: -1 }, limit: 2 });
            },
            children: [
                {
                    find: function(comment, post) {
                        // Find user that authored comment.
                        return Meteor.users.find(
                            { _id: comment.authorId },
                            { limit: 1, fields: { profile: 1 } });
                    }
                }
            ]
        }
    ]
});

//客户

Meteor.subscribe('topTenPosts');

最主要的是它是reactive

【讨论】:

    猜你喜欢
    • 2015-08-05
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-14
    相关资源
    最近更新 更多