Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 铁路由器在waitOn功能中选择不同的订阅-Meteor_Javascript_Meteor_Iron Router - Fatal编程技术网

Javascript 铁路由器在waitOn功能中选择不同的订阅-Meteor

Javascript 铁路由器在waitOn功能中选择不同的订阅-Meteor,javascript,meteor,iron-router,Javascript,Meteor,Iron Router,我想根据文档中的布尔属性(模式)来决定用户获得的订阅,但我在设计方法上有问题。如果我使用this.data()和waitOn函数中的If子句,我将得到404页面,因为我首先需要订阅 这是我的密码: this.route('gamePage', { path: '/game/:slug/viewGame/:produceDate?/:releaseDate?', onBeforeAction: [memberFilter], waitOn: func

我想根据文档中的布尔属性(
模式
)来决定用户获得的订阅,但我在设计方法上有问题。如果我使用
this.data()
waitOn
函数中的If子句,我将得到404页面,因为我首先需要订阅

这是我的密码:

this.route('gamePage', {
        path: '/game/:slug/viewGame/:produceDate?/:releaseDate?',
        onBeforeAction: [memberFilter],
        waitOn: function() {
            var game = this.data();
            if (game.mode) {
                if (this.params.produceDate && this.params.releaseDate) {
                    return [Meteor.subscribe('singleGame', this.params.slug),
                        Meteor.subscribe('authorsMode', this.params.slug, this.params.produceDate, this.params.releaseDate)];
                } else {
                    return [Meteor.subscribe('singleGame', this.params.slug), Meteor.subscribe('console', this.params.slug)];
                }
            } else {
                return [Meteor.subscribe('singleGame', this.params.slug),
                    Meteor.subscribe('authors', this.params.slug)];

            }
        },
        data: function() {
            return Games.findOne({slug: this.params.slug});
        }

任何帮助都将不胜感激。

要直接实现这一点,您需要Iron Router让您拥有两个级别的订阅,第一个级别使数据可用于下一个级别的订阅。此功能目前不存在。我刚刚向IR repo提交了一份请求此功能的报告

解决方法是为每种情况提供不同的URL,并进行中间重定向:

this.route('gamePage', {
  path: '/game/:slug/viewGame/:produceDate?/:releaseDate?',
  onBeforeAction: function() {
    var game = this.data();
    var params = {
      slug: this.params.slug,
      produceDate: this.params.produceDate,
      releaseDate: this.params.releaseDate
    }
    if (game.mode) {
      if (this.params.produceDate && this.params.releaseDate) {
        this.redirect('gamePageOption1', params);
      } else {
        this.redirect('gamePageOption2', params);
      }
    } else {
      this.redirect('gamePageOption3', params);
    }
  },
  waitOn: function() {
      return [Meteor.subscribe('games')];
  },
  data: function() {
      return Games.findOne({slug: this.params.slug});
  }
}
如果您使用的是IR v<0.9.3,请注意有关中间重定向的问题:。如果是这种情况,则必须在Meteor.defer中执行重定向:

Meteor.defer(function() {
    this.redirect(...)
})

谢谢你的帮助!我不能100%肯定我是否正确理解你的方法。因此,您建议使用不同的路由,如
this.route('gamePageOption1')
this.route('gamePageOption2')
,具有相同的URL,但具有不同的订阅?没错,就是这样。一个url可以是
/game/:slug/viewGame/:produceDate?/:releaseDate?/withMode
另一个
/game/:slug/viewGame/:produceDate?/:releaseDate?/noMode
,每个url都有不同的订阅。我只想补充一点,我找到了一个更好的方法(出于我的目的)。我没有重定向到不同的路由,而是将语句替换为
this.subscribe('collectionName',this.params.slug).wait()。为我工作!这实际上是一个很好的解决方案!谢谢分享。我建议添加该评论作为答案,并将其标记为已接受的答案。