Dynamically add routes at runtime?


#1

We need to load data from the server to determine what some routes will be.

Tried to do Router.map inside an afterModel hook in a route and found that it does not successfully add the route.

Do we need to fetch data in an initializer to do this or… is there some other method to call to add routes at request time? We ideally need a way that will be compatible with fastboot as well. But basically some routes need to be defined based on the URL being hit.

Ember Engines seem to need this lazy-loading route capability as well, however it seems like they can hook in during app initialization, so not sure this will be sufficient for us…


#2

When you say:

We need to load data from the server to determine what some routes will be

Can you give an example of the kind of route you’re trying to build?


#3

It’s kind of a long story. Suffice it to say we won’t know what the paths are until the first page is requested. Basically different routes based on what subdomain is hit.


#4

Currently we’re using a wildcard/glob route to support it but {{link-to}} and transitionTo don’t work properly. Here’s the topic I posted about that.


#5

https://github.com/jamesarosen/ember-i18n/issues/310#issuecomment-138411200

But just note, if you are using FastBoot you will run into trouble with this approach because of how Ember keeps a reference of all the Router.map callbacks on the constructor which will leak between requests. If you’re not using FastBoot, it’s safe.

Depending on your use case, you may be able to do what you want within the Router.map callback via https://github.com/jasonmit/ember-routermap-inject-service