【发布时间】:2014-07-09 11:25:42
【问题描述】:
当我更新集合时,我的客户端订阅例程没有刷新:
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 });
});
我为我的客户订阅了两个集合发布,当它获取所有数据时,它会创建一个包含一些我需要的东西的会话对象。
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
}
});
},
当更新成功时,客户端订阅应该更新在我的会话对象中创建一个新对象,对吧?因为集合已更改,所以服务器中的发布已刷新...但它不会刷新,我评论 // UNDEFINED 而不是返回我的新对象正在返回 UNDEFINED
我不知道这是 Meteor 的行为还是我遗漏了一些东西...我尝试更新传递给发布方法 decisionCursor 的参数以强制更新,但没有发生任何事情 Session.set('decisionCursor', Session.get('decisionCursor'));
编辑:似乎如果我使用Session.set('decisionCursor', Session.get('decisionCursor') + 1);(请注意+1)它会刷新但不在结果函数内,如果我再次点击它会检测到添加了新对象...但我需要在结果函数中刷新它(在我的home.js 点击事件中)
【问题讨论】:
标签: javascript node.js mongodb meteor