您可以采用的方法是在模板的created
函数中订阅,然后在呈现模板时首先检查每个订阅的ready()
,如果它们不全都是真的,则渲染加载显示。
<template name="templateWithSubscription">
{{#if ready}}
<!-- actual content -->
{{else}}
loading...
{{/if}}
</template>
Template.templateWithSubscription.created = function() {
this.subscriptions = [
Meteor.subscribe(/* ... */),
Meteor.subscribe(/* ... */),
/* ... */
];
};
Template.templateWithSubscription.destroyed = function() {
_.each(this.subscriptions, function (sub) {
sub.stop();
});
};
Template.templateWithSubscription.helpers({
ready: function() {
return _.all(Template.instance().subscriptions, function (sub) {
return sub.ready();
});
}
});
相似的方法也可以用于其它数据源(例如方法)。
我该如何做多个订阅并阻止它们销毁? – user2858924 2014-09-02 11:18:28
我编辑它以显示多个订阅。 – user3374348 2014-09-02 11:37:08
感谢您的快速响应 – user2858924 2014-09-02 17:11:26