Meteor发布订阅不是被动的 [英] Meteor publish subscribe is not reactive

查看:80
本文介绍了Meteor发布订阅不是被动的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我更新集合时,我的客户订阅程序没有刷新:

My client subscription routines are not refreshing when I update a collection:

server / publish.js

Meteor.publish('decisions', function (decisionCursor) {
    return Decisions.find({ active: true }, { limit: 20, skip: decisionCursor });
});

Meteor.publish('decisionsToModerate', function (decisionCursor) {
    return Decisions.find({ active: false }, { sort: { createdAt: -1 }, limit: 1, skip: decisionCursor });
});

我将我的客户订阅到两个集合出版物,当它获取所有数据时,它会创建一个会话对象我需要的东西。

I subscribe my client to both collection publications and when it fetchs all data it creates a session object with some stuff I need.

client / client.js

Meteor.startup(function () {
    SimpleSchema.debug = true;
    Deps.autorun(function () {
        Meteor.subscribe("decisions", Number(Session.get('decisionCursor')), function () {
            var decisionsVoted = {};
            Decisions.find({
                active: true
            }).forEach(function (decision) {
                var userVoted = Meteor.users.findOne({
                    "_id": Meteor.userId(),
                    "profile.votes.decision": decision._id
                }) != null ? Meteor.users.findOne({
                    "_id": Meteor.userId(),
                    "profile.votes.decision": decision._id
                }) : false;

                var ipVoted = Votes.findOne({
                    "ip": headers.get('x-forwarded-for'),
                    "votes.decision": decision._id
                }) != null ? true : false;
                if (ipVoted || userVoted)
                    decisionsVoted[decision._id] = {
                        voted: true,
                        blue: decision.blueTotal,
                        red: decision.redTotal,
                        bluePer: Math.round(decision.blueTotal * 100) / (decision.blueTotal + decision.redTotal),
                        redPer: Math.round(decision.redTotal * 100) / (decision.blueTotal + decision.redTotal)
                    };

            });
            Session.set('decisionsVoted', decisionsVoted);
        });
        Meteor.subscribe("decisionsToModerate", Number(Session.get('decisionCursor')));
    });
});

client / lib / environment.js

activeDecisions = function() {
    var decisions = Decisions.find({active: true});
    console.log(decisions.fetch().length);
    return decisions;
};
moderateDecisions = function() {
    return Decisions.find({active: false});
};

client / views / home / home.js

'click': function (event) {
    event.preventDefault();
    var decisionId = Session.get("selected_decision");
    var hasVoted = Session.get('decisionsVoted')[decisionId] ? Session.get('decisionsVoted')[decisionId].voted : false;

    Meteor.call("vote", decisionId, 'blue', hasVoted, function (error, result) {
        if (!error && result === true) {
            console.log(Session.get('decisionsVoted')[decisionId]); // UNDEFINED
        }
    });
},

更新成功后,客户端subscriptioun应更新在我的内容中创建一个新对象会话对象,对吗?因为集合已更改,所以刷新服务器中的发布...但它没有刷新,我评论 // UNDEFINED 而不是返回我的新对象返回 UNDEFINED

When updating is successful, client subscriptioun should update creating a new object in my session object, right? Because collection is changed so publish in server is refreshed... But it does not refresh and what I commented // UNDEFINED instead of returning my new object is returning UNDEFINED

我不知道这是Meteor的行为还是我错过了什么......我试过更新参数传递给发布方法 decisionCursor 以强制更新但没有任何事情 Session.set('decisionCursor',Session.get('decisionCursor') );

I don't know if that's the behaviour of Meteor or I'm missing something... I've tried to update parameter passed to publish method decisionCursor in order to force update but nothing hapens Session.set('decisionCursor', Session.get('decisionCursor'));

编辑:似乎如果我使用 Session.set(' decisionCursor',Session.get('decisionCursor')+ 1); (注意+1)它被刷新但不在结果函数内,如果我再次点击它会检测到添加了新对象..但是我需要在结果函数中刷新它(在我的 home.js 点击事件中)

it seems that if I use Session.set('decisionCursor', Session.get('decisionCursor') + 1); (note that +1) it is refreshed but not inside result function, if I click again it detects that new object is added... But I need it to be refreshed inside result function (inside my home.js click event)

推荐答案

这篇(优秀)文章可能有所帮助。换句话说:

This (excellent) article might help. To paraphrase:


...在服务器上,Meteor的反应性仅限于Meteor.publish()函数返回的游标。这样做的直接后果是,与客户端不同,只要数据发生变化,代码就不会神奇地重新运行。

...on the server, Meteor’s reactivity is limited to cursors returned by Meteor.publish() functions. The direct consequence of this is that unlike on the client, code will not magically re-run whenever data changes.

这篇关于Meteor发布订阅不是被动的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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