iron-meteor / iron-router

A client and server side router designed specifically for Meteor.
MIT License
1.98k stars 412 forks source link

Unexpected behavior with Router.go() and Session Variables #796

Closed scottmcpherson closed 10 years ago

scottmcpherson commented 10 years ago

I have a route that is called with Router.go() on a click event:

Router.go('activity.create', { _id: Session.get('project') })

I then set session variable to handle conditionally rendering a form:

onBeforeAction: function () {
    Session.set('newActivity', true);
}

and I'm setting the Session to false when the user navigates away from that route:

onStop: function () {
    Session.set('newActivity', false);
}

The first time I call Router.go(), the Session variable is correctly set and the form renders as expected. And when I hit the back arrow key, the form disappears and the Session is set to false as expected. But when I hit the browser forward arrow key, the Session does not get correctly set back to true and the form does not render.

Another thing that I noticed: when I call Router.go() for the same route multiple times with the same exact parameters, the routes seemed to be getting pushed even though it's already on that identical route. In other words, if I click on the button three times—the one that's responsible for calling the form rendering route, I have to click on the browser's back arrow key three times to get back to the previous route.

Also, the above scenario happens with the following route parameter structure:

this.route('activity.create', {path: '/projects/:_id/activity/create'}); 

Originally, however, I was trying to call the route with a sandwiched optional parameter

this.route('activity.create', {path: '/projects/:_id/activity/:taskId?/create'});

Any time the browser was refreshed, and the button was clicked again, it would add an additional create to the end of the url: http://localhost:3000/projects/KnmcJY6jdZgyBzKX2/activity/create/create

Is it required to put optional parameters at the end of the path?

tmeasday commented 10 years ago

Hi Scott,

There are a few things going on here:

  1. The non-setting of the session var on "forward" is surprising. Can you possibly create a reproduction of the form https://github.com/meteor/meteor/wiki/Contributing-to-Meteor#reporting-a-bug-in-meteor. That'd help.
  2. The behaviour of re-hitting the same route is intentional and was discussed somewhere along the line I think.
  3. The last question sounds like a different issue, can you perhaps create a separate ticket for it? Thanks.
scottmcpherson commented 10 years ago

Hey Tom, I should have some time tomorrow to throw together a simple reproduction of this. I'll cc you once I get it uploaded to GitHub. And I'll get a separate ticket made for the last question.