这是我的旧版本的新版本question http://pastebin.com/xG0zLE0X:
感谢 Tom Coleman 的帮助,我终于弄清楚了如何正确检查订阅是否就绪()。
我当前的代码结构如下所示:
/client/app.js:
eventsHandle = null;
groupsHandle = null;
// ...
// First Deps.autorun():
// Does not depend on any Session var, should just run every time
Deps.autorun(function() {
eventsHandle = Meteor.subscribe("events", function() {
console.log('Deps.autorun(): Events loaded');
});
});
// Second Deps.autorun():
// contains all subscriptions which are dependent on my Session var "ehash"
Deps.autorun(function() {
if(Session.get('ehash'))
groupsHandle = Meteor.subscribe("groups", Session.get('ehash'), function() {
console.log('Deps.autorun(): Groups loaded with ehash: ' + Session.get('ehash'));
});
});
// ...
然后,我可以查看名为的文件夹中所有模板内容的特定 .js 和 .html 文件:
/client/views/
--> <page>.js:
Template.x.dataLoaded = function() {
if(Session.get('ehash'))
if(eventsHandle && groupsHandle && eventsHandle.ready() && groupsHandle.ready()) {
console.log('All data loaded!');
singleevent = Events.find({ehash: Session.get('ehash')}).fetch()[0];
return true;
}
}
这个帮手dataLoaded
基本上将所有内容包装在相应的模板中,并在以下情况下显示内容:dataLoaded
返回 true 或显示加载微调器。
问题是,在许多情况下这不起作用,因为此 dataLoaded 代码仅运行一次。因此,如果在运行 dataLoaded 时两个句柄未就绪(),则内容将永远不会显示。在这种情况下,我仍然看到所有 console.log 来自 app.js 文件(Deps.autorun() 的东西),但日志“所有数据已加载!”永远不会得到回应。
所以我的问题是:如何触发此代码的重新运行dataLoaded
再次运行,内容最终会显示吗?
此致