javascript - 路由器不等待订阅

标签 javascript meteor iron-router

我的问题是我有两个相似的路径,第一个路由器等待我的订阅并渲染整个模板,但第二个路由器立即渲染而没有加载,并且传递的数据导致错误(因为没有订阅的集合)然而)。 我将我的代码粘贴到此处,第二个代码由于传递的模板和数据而有所不同,但其余部分实际上是相同的。 我刚刚开始使用铁路由,也许有人可以告诉我错误在哪里?

Router.map(function() {
        this.route('/', {
            onBeforeAction: function() {
                if (Meteor.user()) {
                    if (Meteor.user().firstLogin)
                        this.render("firstLogin");
                    else
                        Router.go('/news');
                } else {
                    this.render("start");
                }
            },
            waitOn: function() {
                return Meteor.subscribe('allUsers');
            },
            onAfterAction: function() {
                document.title = "someTitle";
            },
            loadingTemplate: "loading",
        });
        this.route('users',{
            path:'/user/:_id',
            layoutTemplate: 'secondLayout',
            yieldTemplates: {
                'template1': {to: 'center' },
                'template2': {to: 'top' },
                'template3': {to: 'left' },
                'template4': {to: 'right' },
            },
            waitOn: function(){
                return Meteor.subscribe("allUsers");
            },
            data: function(){
                return Meteor.users.findOne({_id:String(this.params._id)});
            },
            loadingTemplate: "loading",
        });
    });

最佳答案

您正在旧版本中使用iron-router。如果你刚刚开始的话。我建议您使用新的 api。在这种情况下,您可以使用 this.ready() 来检查订阅是否完成

以下是 official guide 中的示例

Router.route('/post/:_id', function () {
  // add the subscription handle to our waitlist
  this.wait(Meteor.subscribe('item', this.params._id));

  // this.ready() is true if all items in the wait list are ready

  if (this.ready()) {
    this.render();
  } else {
    this.render('Loading');
  }
});

关于javascript - 路由器不等待订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27044613/

相关文章:

javascript - Meteor:使用四个结果中的字符串构建一个新对象

meteor - Meteor.js 应用程序模板中的多产量

javascript - 将列表传递给 Parse.Promise.when 但仅获取 promise 链中的第一个元素

javascript - 在JQuery中获取被点击的元素

javascript - Bootstrap-Vue 坏了吗?

node.js - SSL 检查错误 :CERT_UNTRUSTED when adding meteor package

reactjs - react : How to safely display a component according to user role

templates - Meteor:在用数据渲染模板后调用函数

Meteor Iron-router onBeforeAction this.next 未定义

javascript - 元素在可滚动容器中是否可见