Meteor:使用 ReactiveVar 创建一个加载小部件以订阅用户集合

Meteor: Create a loading widget using ReactiveVar for subscribing to users collection

我的主页模板有一个加载功能,非常适合订阅用户集合。不幸的是,加载占据了整个页面,我想在等待用户加载时显示一些一般信息。

我已经使用 ReactiveVar 在我的注册模板的创建帐户功能中实现了这一点,效果很好。

这是在那里完成的:

Template.signup.onCreated(function() {
  Template.instance().isLoading = new ReactiveVar(false);
});

Template.signup.helpers({
  isLoading() {
    return Template.instance().isLoading.get();
  }
});

Template.signup.events({
  'submit #signup-form': function(event, template) {
    // Not important stuff here
    template.isLoading.set(true);
    Accounts.createUser({
      // More not important stuff here
    }, function(err) {
      // Even more not important stuff here
      template.isLoading.set(false);
    })
  }
});

然后在模板中我有这个:

<template name="signup>
  {{#unless isLoading}}
    <!-- Stuff -->
  {{else}}
    <!-- More Stuff -->
    {{> loading}}
  {{/unless}}
</template>

这非常有效,但我想在我订阅用户的主页上做同样的事情。有没有办法做类似的事情?我已经设置了主页模板调用和其他内容。但是我不知道加载用户数据的模板事件怎么做。

这就是我的基本情况:

Template.home.onCreated(function() {
  Template.instance().isLoading = new ReactiveVar(false);
});

Template.home.helpers({
  user: function() {
    return Meteor.users.find({});
  },
  isLoading() {
    return Template.instance().isLoading.get();
  }
});

Template.home.events({
  //What do I do here????
});

我很感激能得到的任何帮助:)

编辑:这是我的 Meteor 发布代码:

Meteor.publish('users', function() {
  return Meteor.users.find({}, {fields: {username: 1, emails: 1, profile: 1}});
});

我想你想使用 Blaze 助手 Template.subscriptionsReady:

<template name="home">
    {{#if Template.subscriptionsReady}}
        <!-- Your template code -->
    {{else}}
        <!-- Your loading code -->
    {{/if}}
</template>

这里是link相关文档:http://docs.meteor.com/#/full/Blaze-TemplateInstance-subscribe