Dynamically add routes at runtime?

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…

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?

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.

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.


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 GitHub - jasonmit/ember-routermap-inject-service