kadirahq / flow-router

Carefully Designed Client Side Router for Meteor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: manual initialization

DominikGuzei opened this issue Β· comments

Hey @arunoda!
Awesome package, works great for my current customer project πŸ˜‰

There is only one thing that caused me some trouble: I can't control the initialization of flow-router.
i want routing to start exactly when the app is ready for it and not when your Meteor.startup hook is called internally. This works perfectly with iron router when telling it "not to autorun", could you add a similar api?

I even tried overriding the Router.prototype.initialize method but then i got other weird errors.

Can I have the usecase for this? Then I can think what are the possible things we can do.

It's pretty simple: I want to initialize and run my custom app within a Meteor.startup block but flow-router is starting to route before that. So i can't reference my running app within the first route.

So here is the code i have which causes problems:

Meteor.startup -> 
  TestApp.app = new TestApp.Application()
  TestApp.app.start()

FlowRouter.route '/test/:code',
  name: 'testRoute'
  triggersEnter: [(context, redirect) -> redirect "#{context.path}/search"]

FlowRouter.route '/test/:code/search',
  name: 'testSearchRoute'
  action: (params) ->
    TestApp.app.publish new TestApp.SearchRouteVisited code: params.code

visiting test/123 redirects to the search route test/123/search which publishes an event to the app that triggers other stuff in the app. The problem here is that TestApp.app is not yet defined here because flow-router enters the action before my custom Meteor.startup hook was triggered.

Okay. We just faced a similar issue with flow-layout and we are fixing it with rendering it inside a Meteor.defer.

I hope that'll fix your problem too using a similar patten. It's a kind of tricky on how to give an handler to initialise Flow Router. (I mean in API wise)

I think I've the API in mind. We can introduce something called FlowRouter.wait(). Then it won't automatically initialise, but allow you to do it via FlowRouter.initialize() when it's ready.

Ok great :-) i am happy about any solution that solves this problem!

If this solved, I think this may be the fast feature request implementation I have done :D

Awesome, thanks so much! πŸ‘

Hope we fixed this :)

Yeah it works now! πŸ˜‰ thanks again

I have another use case that I can't seem to get working with the current implementation of wait() and initialize(). I'm using react, jsx, universe:modules for import/export, etc. My main.js file looks like this:

FlowRouter.wait();

Meteor.startup(function() {
  System.import('client/scripts/routes');
  FlowRouter.initialize();
});

If I'm correct, FlowRouter doesn't allow the addition of FlowRouter.route()s later on. Or am I doing something wrong? What I would like to do is call FlowRouter.wait(), load my routes via System.import in Meteor.startup that way all of my react components are loaded/ready, and then call FlowRouter.initialize(). The reason I need to do this is since I'm rendering react components in the actions obviously.

You can add routes later on via FlowRouter.route(). Above code looks good to me. I'd like to see a sample project with your problem.

It sounded like it would work to me too, I may just be doing else wrong; definitely in the experimentation phase of this setup.

https://github.com/zachdixon/socialcentiv-meteor/tree/modules

I think you're right that this isn't a FlowRouter issue, I've tried it with React Router as well and got the same errors. It must be something with my app-deps package that I got from here
I've tried updating package versions which got me this error:
[Universe Modules]: Module app-deps does not exist! You will probably see other errors in the console because of that.

Sorry to bother you, still trying to figure out the issue.

FlowRouter.wait() just saved my day! Thanks! :)