简体   繁体   English

路由器不等待订阅

[英]Router doesn't wait for subscription

My problem is that I have two similar paths and in first one router waits for my subscriptions and renders whole template, but the second one is rendering right away with no loading and data passed is causing errors(since there is no collection subscribed yet). 我的问题是我有两个相似的路径,第一个路由器等待我的订阅并呈现整个模板,但是第二个路由器立即呈现而没有加载,并且传递的数据导致错误(因为还没有订阅的集合)。 I paste my code here, the second one is different because of template and data passed but the rest is practically the same. 我将代码粘贴到此处,第二个代码由于传递的模板和数据而有所不同,但其余代码实际上是相同的。 I'm just starting with iron-routing, maybe someone can tell me where is mistake? 我只是从铁路线开始,也许有人可以告诉我哪里出了错?

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",
        });
    });

You are using iron-router in the lagacy. 您正在使用Lagary路由器。 If you're just starting it. 如果您只是开始。 I recommend you use the new api. 我建议您使用新的API。 In that case, you can use this.ready() to check the subscription is finished or not 在这种情况下,您可以使用this.ready()来检查订阅是否完成

Following is the example from the 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');
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM