【问题标题】:Meteor: Create a loading widget using ReactiveVar for subscribing to users collectionMeteor:使用 ReactiveVar 创建加载小部件以订阅用户集合
【发布时间】:2016-05-02 05:41:32
【问题描述】:

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

我已经在我的注册模板中使用 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}});
});

【问题讨论】:

    标签: javascript templates meteor loading reactive-programming


    【解决方案1】:

    我想你想使用 Blaze 帮助器 Template.subscriptionsReady:

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

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

    【讨论】:

    • 似乎没有运行在 else 语句中运行的代码:/
    • 嗯,您是专门想获取所有用户在家中的信息还是仅获取当前登录的用户?
    • 是的,我正在尝试获取所有存在的用户。
    • Meteor.publish('users', function() { return Meteor.users.find({}, {fields: {username: 1, emails: 1, profile: 1}}); } );现在我刚刚意识到我在发布代码中遗漏了角色字段。谢谢你帮我看看有什么问题。我将使用该代码更新我的问题,您可以从那里回答要做什么。
    • 它有效,编辑您的答案以包含修复它的注释
    猜你喜欢
    • 2015-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-11
    • 2021-07-18
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    相关资源
    最近更新 更多