更新集合时,我的客户端订阅例程不会刷新:
服务器/ 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.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')));
});
});
客户端/ 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});
};
客户端/视图/家/ 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
我不知道这是Meteor的行为还是我遗漏了一些东西……我试图更新传递给发布方法decisionCursor的参数以强制更新但是没有任何东西支持Session.set(‘decisionCursor’,Session.得到( ‘decisionCursor’));
编辑:似乎如果我使用Session.set(‘decisionCursor’,Session.get(‘decisionCursor’)1); (注意1)它被刷新但不在结果函数内部,如果我再次点击它会检测到添加了新对象…但是我需要在结果函数内刷新它(在我的home.js点击事件中)
解决方法:
This (excellent) article可能有所帮助.换句话说:
…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.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。