Search code examples
javascriptmeteoriron-router

Make Iron Router reload page when clicking on the same link


I just started playing around with Meteor and Iron Router to create a simple site. There are a few links on the nav bar of the site and one of them is called random. The idea is that when a user clicks it, a random post would display at the page.

Now Iron Router seems to prevent a page from reloading if it's the same link with the current one. That's a great feature except in my case I need it to reload so the user can see a new post.

Here's the relevant code:

Router.route('/random', function() {
    console.log("running this!");
    this.layout('SinglePostLayout', {
        data: function () { return draw(Posts, {}) }
    });
    this.render('post');

The draw function returns a post in the collection. It's fun from perfect but that's not relevant here. I confirmed the code only ran once by using console.log.

Is there a way around this? I want to preserve the no reloading behaviour for all other links except for the random one. I've searched for answers for a while but couldn't find anything.

Thanks!


Solution

  • Since you are not using dynamic routes to show each post.

    You can use the Location.reload() method from the window object, and use pure Javascript.

      if(Meteor.isClient){
        Template.home.events({
            'click #randomHref':function(){
                document.location.reload(true);
                // Router.go('/') don't work.
            }
        })
    }