javascript - iron-router 在 waitOn 函数中选择不同的订阅 - Meteor

标签 javascript meteor iron-router

我想根据文档中的 bool 属性(mode)来决定用户获得哪些订阅,但我的设计方法存在问题。如果我在 waitOn 函数中使用 this.data() 和 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 来让您拥有 2 个级别的订阅,第一个级别使数据可供下一个订阅级别使用。目前不存在此功能。我刚刚提交了 issue向 IR 存储库请求此功能。

解决此问题的方法是为每种情况使用不同的 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,请注意有关中间重定向的问题:ref1 , ref2 。如果是这种情况,您必须在 Meteor.defer 内进行重定向:

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

关于javascript - iron-router 在 waitOn 函数中选择不同的订阅 - Meteor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25949492/

相关文章:

node.js - 如何正确安装node_rrd?

meteor - 使用 Iron Router 路由到 Meteor autoform 提交的新数据?

javascript - 如何在特定页面上加载文件?

javascript - 使用 Google Apps 脚本解析 XML 元素中的所有值?

javascript - D3 - 使用 D3 的 SVG 符号缩放

javascript - 使用PapaParse和collectionFS在meteor中生成可下载的csv文件

javascript - Meteor.js 与 Iron Router 按钮和模板中的表单不起作用

javascript - Flask JWT扩展设置cookie错误

span 元素的 Javascript 宽度

meteor - 如何在 meteor.js 应用程序中从非 www 重定向到 www