在客户端启动时,我订阅了一些内容:
Meteor.publish("Roles", function(){
return Roles.find();
});
Meteor.startup(function() {
if(Meteor.isClient) {
Meteor.subscribe('Roles');
}
});
角色模板:
Template.roles.helper(function() {
allRoles: function() {
return Roles.find().fetch();
}
})
<template name="roles">
<div>
{{#with allRoles}}
<label>{{> role }}</label>
</div>
</template>
问题是有时roles
模板在Roles
准备好之前呈现。
如何应对这种情况?
答案 0 :(得分:2)
您可以在模板上进行订阅,然后使用Template.subscriptionReady
帮助程序创建条件,以便在加载订阅时显示加载面板,如下所示:
Template.roles.onCreated(function () {
this.subscribe("Roles");
});
Template.roles.helper(function() {
allRoles: function() {
return Roles.find().fetch();
}
})
<template name="roles">
<div>
{{#if Template.subscriptionsReady}}
{{#with allRoles}}
<label>{{> role }}</label>
{{else}}
Loading...
{{/if}}
</div>
</template>
这将替换您的其他订阅,并且可以将这些订阅添加到每个模板的每个onCreated方法中,以便每个模板都有订阅。
答案 1 :(得分:0)
有一些常见的方法来处理它。您可以使用防护或使用铁路由器waitOn
功能。如果你得到任何结果,你只需要从帮助者那里返回数据:
allRoles: function() {
var roles = Roles.find();
//explicit version
if (roles.count()) return roles
//implicitly works as well here because you're returning null when there are no results
return roles
}
在这种情况下你不需要fetch(),因为#with适用于游标。如果您遇到需要首先获取的情况,因为您要返回部分数据,请先检查是否有结果,然后才返回结果。
如果您将此作为路线的一部分使用,也可以使用铁路由器的waitOn选项。