2014-11-04 77 views
3

我升级了我的应用程序到流星1.0并更新了我的router.js,因为我不能使用.wait()anymore。然而,现在我的未找到的页面在“真实页面”出现之前弹出了一秒钟。我怎样才能解决这个问题?未找到页面弹出一秒 - 流星 - 铁 - 路由器

这里是我的代码:

this.route('gamePage', { 
     path: '/game/:slug/', 
     onBeforeAction: [function() { 
      this.subscribe('singlePlayer', this.params.slug); 
      var singlePlayer = this.data(); 
      if (singlePlayer) { 
       if (singlePlayer.upgrade) { 
        this.subscribe('upgrades', this.params.slug); 
       } 
      } 
      this.next(); 
     }], 
     data: function() { 
      return Games.findOne({slug: this.params.slug}); 
     }, 
     waitOn: function() { return [Meteor.subscribe('singleGame', this.params.slug)]} 
    }); 

任何帮助将不胜感激。

+0

什么版本的铁路由器?另外:为什么在'onBeforeAction'回调周围使用数组(方括号)括号?我以前没有看过这种语法。不过,我只使用了铁路路由器0.9.4。 – 2014-11-10 01:13:19

回答

1

尝试使用subscriptions模式代替。

this.route('gamePage', { 
    path: '/game/:slug/', 
    subscriptions: function() { 
     return Meteor.subscribe('singlePlayer', this.params.slug); 
    }, 
    onBeforeAction: function() {  
     var singlePlayer = this.data(); 
     if (singlePlayer) { 
      if (singlePlayer.upgrade) { 
       this.subscribe('upgrades', this.params.slug); 
      } 
     } 
     this.next(); 
    }, 
    data: function() { 
     return Games.findOne({slug: this.params.slug}); 
    }, 
    waitOn: function() { return [Meteor.subscribe('singleGame', this.params.slug)]} 
}); 

然而,重要的是你还包括loading插件采取loadingTemplate的优势。

Router.configure({ 
    loadingTemplate: 'loading' // general purpose loading template 
}); 

// built in plugin.. surprisingly not clearly specified in current docs, but you can dive in the code for plugins. 
// https://github.com/EventedMind/iron-router/blob/devel/lib/plugins.js 

Router.onBeforeAction('loading', { only: ['gamePage'] }); // only show loading for pages with subscriptions 

Router.map(function() { 
    this.route('gamePage',{ 
     //... your other options here .. 
     loadingTemplate: 'gamePageLoading', // game Page dedicated loading markup. 
    }); 
}); 

另外还有this.ready()模式,如果你想留在你onBeforeAction实现。

this.route('gamePage', { 
    path: '/game/:slug/', 
    onBeforeAction: [function() { 
     this.subscribe('singlePlayer', this.params.slug); 

     if(this.ready()) { 
      var singlePlayer = this.data(); 
      if (singlePlayer) { 
       if (singlePlayer.upgrade) { 
        this.subscribe('upgrades', this.params.slug); 
       } 
      } 
      this.next(); 
     } else { 
      this.render('loading'); 
     } 

    }], 
    data: function() { 
     return Games.findOne({slug: this.params.slug}); 
    }, 
    waitOn: function() { return [Meteor.subscribe('singleGame', this.params.slug)]} 
}); 

来源:https://github.com/EventedMind/iron-router/blob/devel/Guide.md#subscriptions

我想,因为.wait图案被看作是不必要的链接这种变化是必要的,很容易出现(编码)错误。此外,当重写onBeforeAction时,明确处理.next()现在确保此挂钩的正确时间(并且可能大部分,如果不是全部其他挂钩)。

+0

非常感谢! :) – user3475602 2014-11-10 19:39:21

相关问题