【问题标题】:Meteor JS: Conditional subscriptions, based on passed arguments, in Iron Router?Meteor JS:Iron Router 中基于传递参数的条件订阅?
【发布时间】:2014-01-18 17:17:35
【问题描述】:

这其实是两个问题:

  1. 是否可以在 Iron Router 的 waitOn 选项中有条件地订阅集合?

  2. 是否可以在 Router.go() 中将对象作为参数传入?

在我的应用中创建新帖子时,我正在尝试减少呈现视图的延迟。我尝试传入一个 isNew 属性作为 Router.go() 的参数,但没有运气:

// Router call after creating a new post
Router.go('postPage', {
  _id: id, 
  isNew: true, 
  post: newPostObject
});

// router.js
Router.map(function() {
  this.route('postsList', {
    path: '/'
  });
  this.route('postPage', {
    path: '/:_id',
    waitOn: function() {

      //This returns only _id for some reason.
      console.log(this.params);

      if (this.params.isNew != true) {
        return [
          Meteor.subscribe('singlePost', this.params._id),
          Meteor.subscribe('images', this.params._id),
        ]
      }
    },
    data: function() {
      if (this.params.isNew == true) {
        return this.params.post
      else {
        return Posts.findOne(this.params._id);
      }
    }
  });
});

【问题讨论】:

    标签: javascript meteor iron-router


    【解决方案1】:

    经过一番挖掘,看起来 Iron Router 支持选项哈希作为 Router.go() 方法中的第三个参数:

    Router.go( 'postPage', {_id: id}, {isNew: true} );
    

    要在路由中访问它,您可以使用然后使用this.options。要获取上例中isNew 的值,请使用this.options.isNew

    【讨论】:

      【解决方案2】:

      您只能通过this.params 访问动态路径段。所以要让this.params.isNew 工作,你需要像这样。

      this.route('postPage', {
        path: '/:_id/:isNew',
        waitOn: function() {}
        ...
      });
      

      【讨论】:

      • 我做了一些挖掘,看起来我可以这样做:Router.go('postPage', {_id: id, post: newPostObject }, {isNew: true} ); 然后,在路由器中,我可以通过this.options.isNew 访问isNew
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 2016-05-25
      • 2015-07-07
      • 2017-06-07
      相关资源
      最近更新 更多