admin管理员组

文章数量:1391951

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!

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!

Share Improve this question asked Apr 4, 2015 at 18:00 GnijuohzGnijuohz 3,3646 gold badges34 silver badges48 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

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.
        }
    })
}

本文标签: javascriptMake Iron Router reload page when clicking on the same linkStack Overflow