Iron Router - rerun route

Is there a way to rerun an Iron Router route?
So that its hooks (onBeforeAction,onAfterAction, waitOn) run again?

I want to call this in a template event.
I’ve been hearing stuff about Router.rerun() but that does not seem to work.

cheers,
godo

Well I don’t understand all, but if you want to get current router out of Router Controller can be this: Router.current(). So, if you have a Router controller, and a function inside that, in Template hooks or events can run this function: Router.current().myFunction(); and is done. I think you not need rerun your router to run a function using the Router controller.

You’re probably not doing something right. In most all cases you shouldn’t have to manually re-run the router. Could you elaborate more on what you’re doing and perhaps we can work out a better way?

You should also be able to change a session value to trigger a reactive change (perhaps with Date.now() ).

I’ve found that whenever possible it’s helped me to move this logic from the router and into the topmost template instead.

Thanks for the help!
This is what I got:

this.route('Random', {
    path: '/random',
    template: 'Random',
    waitOn: function() {
      return Meteor.subscribe('all-post-ids');
    },
    onBeforeAction: function() {
      // can't use skip random
      // http://stackoverflow.com/questions/2824157/random-record-from-mongodb

      // put all post id's in array
          var array = Posts.find({},{fields: {_id: 1}}).fetch();
          // randomly select field from array
          var randomId = array[Math.floor(Math.random()*array.length)]._id;
          Session.set('random', randomId);
          // get all data of the selected post
          Meteor.subscribe('one-post', randomId);
          // get comments of the selected post
          Meteor.subscribe('limit-comments', randomId, 5);
          this.next();   
        },
        data: function() {
           return Posts.findOne({_id: Session.get('random')});
        }
});

solved it:

I just setup a method on the server that returns a random id.
And in the template helper:

    Meteor.call('randomPost', function (error, result) {
      if (error) {
        console.log(error);
      } else {
        Router.go('Random', {_id: result});
      }
    });

router:

this.route('Random', {
    path: '/post/:_id',
    template: 'Random',
    waitOn: function() {
      return Meteor.subscribe('post', this.params._id);
    },
    data: function() {
      return Post.findOne(this.params._id);
    }
});
3 Likes

Glad you solved it, looks much cleaner now!

1 Like

OH MY GOD THANK YOU. This is the most confusing part about using IronRouter…

Edit: Nevermind still friggin complicated… :confused: