kadirahq / flow-router

Carefully Designed Client Side Router for Meteor
MIT License
1.09k stars 194 forks source link

FlowRouter.getParam returns undefined after a FlowRouter.go #486

Closed jensljungblad closed 8 years ago

jensljungblad commented 8 years ago

I have the following, most of which I grabbed from the Meteor Guide: http://guide.meteor.com/data-loading.html#organizing-subscriptions

Template.lobby.onCreated(function() {
  this.gameId = () => FlowRouter.getParam("_id");

  this.autorun(() => {
    var game = Games.findOne(this.gameId());

    if (game.started) {
      FlowRouter.go("game", { _id: game._id });
    }
  });
});

Template.lobby.helpers({
  game: function() {
    return Games.findOne(Template.instance().gameId());
  }
});

Now, when game.started turns true, the autorun executes the FlowRouter.go. When this happens, apparently FlowRouter.getParam("_id") becomes undefined, even before the new page has rendered. This leads my game helper to become undefined as well which leads to a bunch of crashes in my templates which expect there to be a game context present.

Is this a bug in FlowRouter or am I supposed to do this differently?

arunoda commented 8 years ago

I'd like to have a sample repo in this case. Could you try this with the kadira:flow-router-ssr instead of Flow Router? New development is going there.

jensljungblad commented 8 years ago

@arunoda Where can I find the docs for the SSR version? I suspect I need to turn off the actual SSR part since I'm using Blaze and not React..

jensljungblad commented 8 years ago

Sorry, my mistake, I was running the router on the server as well as the client, hence the errors. Using the SSR branch seems to have solved the issue!

krizka commented 8 years ago

I have a sample repo. You can see the behaviour by console messages while switching within routes in first line. Please look at it. We first receiving old route param in new route, and then new one, but template helper already was called with bad data. https://github.com/krizka/flow-rerender/tree/master/client

krizka commented 8 years ago

so here we get situation, when FlowRouter.current().params.id == '1', and FlowRouter.getParam('id') == '2' In addition to previous answer, wanted to say, that changing const id = FlowRouter.getParam('id');

FlowRouter.watchPathChange();
const id = FlowRouter.current().params.id;

Working better and always returned right params values, but autorun called twice anyway.

arunoda commented 8 years ago

@krizka I'll have a look on this.

krizka commented 8 years ago

Thanks. Sorry, can't deploy to meteor.com, it says 'temporary unavailable'.

arunoda commented 8 years ago

Hey @krizka I can't find anything wrong your code. May be you should post a new issue with more info.

That's because the original issue of @jensljungblad is solved.

krizka commented 8 years ago

Ok. Thanks. I will try with new version and open new if not solved.

krizka commented 8 years ago

It is still there in 3.9.3. Will open new issue now. Please see in short time.

krizka commented 8 years ago

528 created.