Chanpory
Chanpory

Reputation: 3095

Meteor JS: Conditional subscriptions, based on passed arguments, in Iron Router?

This is really two questions:

  1. Is it possible to conditionally subscribe to collections within Iron Router's waitOn option?

  2. Is it possible to pass in objects as an argument in Router.go()?

I am trying to reduce the delay in rendering a view, when creating a new post within my app. I tried passing in an isNew property as an argument for Router.go(), but had no luck:

// 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);
      }
    }
  });
});

Upvotes: 0

Views: 480

Answers (2)

Chanpory
Chanpory

Reputation: 3095

After some digging, it looks likes Iron Router supports an options hash as a third argument in the Router.go() method:

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

To access it in the route, you can use then use this.options. To get the value of isNew in the above example, use this.options.isNew.

Upvotes: 1

pahan
pahan

Reputation: 2453

you can only access Dynamic Path Segments by this.params. so to get this.params.isNew working you need to have it like this.

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

Upvotes: 0

Related Questions