路由器不等待订阅

时间:2014-11-20 16:27:27

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

1 个答案:

答案 0 :(得分:1)

你正在使用铁路由器。如果你刚刚开始它。我建议你使用新的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');
  }
});