首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Meteor.disconnect()和Meteor.reconnect()模拟反应性

使用Meteor.disconnect()和Meteor.reconnect()模拟反应性
EN

Stack Overflow用户
提问于 2016-03-05 07:08:36
回答 1查看 172关注 0票数 0

我知道这很可怕,但我不知道如何在服务器上获得反应性。我也在使用发布复合包:https://github.com/englue/meteor-publish-composite

我使用的是Iron Router,其中layout.html是主模板。在layout.js中,我有:

代码语言:javascript
复制
Template.layout.created = function(){
    this.autorun(()=>{
        this.subscribe('currentUser')
        this.subscribe('friendsRecordsAnswers')
        this.subscribe('myRecordsGamesRounds')
        this.subscribe('queries')
    })
}

下面是我的一个出版物的例子:

代码语言:javascript
复制
Meteor.publishComposite('myRecordsGamesRounds', function(){
    if(this.userId){
        return {
            find(){
                return Records.find({userId: this.userId});
            },
            children:[{
                find(record){
                    return Games.find({_id: record.gameId})
                },
                children:[{
                    find(game){
                        return Rounds.find({gameId: game._id});
                    }
                }]
            },
            ]
        };
    }
})

这个应用程序是一个你可以邀请其他用户的游戏。但是,当新用户注册时,现有用户必须刷新页面,然后才能邀请他们参加游戏。我目前有一个运行Meteor.disconnect() & Meteor.reconnect()的2000ms的setInterval,到目前为止它实际上工作得很好。但我相信有更好的方法。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2016-03-05 09:41:58

在github示例中,包含两个级别的子对象,您可以看到第二个级别的子对象引用了两个级别的父对象:commentpost

代码语言:javascript
复制
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 } });
                    }
                }
            ]
        }
    ]
});

你没有在你的代码中这样做,而且你本质上只传递了第一级的子引用,而不是根引用。所以我认为你只需要做:

代码语言:javascript
复制
Meteor.publishComposite('myRecordsGamesRounds', function(){
    if(this.userId){
        return {
            find(){
                return Records.find({userId: this.userId});
            },
            children:[{
                find(record){
                    return Games.find({_id: record.gameId})
                },
                children:[{
                    find(game, record){
                        return Rounds.find({gameId: game._id});
                    }
                }]
            },
            ]
        };
    }
})

我会认为,因为你将根传递给最底层的子代,所以它会回头看它。

我可能在这点上错了。我曾经使用过这个包,但不久前就放弃了,因为一旦你想做搜索,你就会得到一个巨大的混乱。

我更喜欢使用https://github.com/peerlibrary/meteor-reactive-publish,它更简洁,可以让你在代码中做你想做的事情,而不是嵌套对象。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35807732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档