In an Aurelia app, I've defined a simple route like this:
configureRouter(config: RouterConfiguration, router: Router) {
config.title = 'Marino';
config.map([
{ route: ['', 'home'], name: 'home', moduleId: './pages/home/home', nav: true, title: 'Home' },
{ route: 'colors', name: 'colors', moduleId: './pages/colors/overview', nav: true, title: 'Colors' }
]);
this.router = router;
}
This works perfectly as all the examples mention, by implementing repeat.for and href.bind like this:
<ul class="main-navigation">
<li repeat.for="row of router.navigation" class="${row.isActive ? 'active' : ''}">
<a class="btn btn-primary" href.bind="row.href">${row.title}</a>
</li>
</ul>
The challenge in my scenario is that I want to dynamically render routes with submenu-items to this menu as well. Something like this:
<ul class="main-navigation">
<!-- the regular 'regular' menu and works just fine -->
<li repeat.for="row of router.navigation" class="${row.isActive ? 'active' : ''}">
<a class="btn btn-primary" href.bind="row.href">${row.title}</a>
</li>
<!--
below is the pickle; a different kind of element (non-clickable),
but with child elements
-->
<li class="main-navigation-dropdown">
<a class="btn btn-primary">Menu with Submenu-items</a>
<div class="horizontal-dropdown-menu">
<a class="btn btn-primary sideline">Submenu 1</a>
<a class="btn btn-primary sideline">Submenu 2</a>
<a class="btn btn-primary sideline">Submenu 3</a>
</div>
</li>
</ul>
What puzzles me are two things:
- How do I properly define the submenu-items in the route config?
- How can I conditionally render each route as either a regular (clickable) route, or as a non-clickable item with submenu's?
I've looked in the RouteConfig docs but can't seem to find any info on 'nested' subroutes. The Aurelia Getting Started does provide info about child routes, but all the samples seem to me to be related to displaying "other", or second menu's on another component.
I'm sure it's quite trivial, but I just can't seem to get a fix on it.
I solved this problem with a ValueConverter. This is only for two levels, but with a little change it could support more.
Routes - settings.parentMenu defines under which menu it will appear.
subMenu.js - Groups the submenus under the parent
nav-bar.html - pipe the router.navigation into the subMenu value converter and then check for children when binding the submenu.
Whether this is the correct way to do it I don't know, but it's the least "hacky" way I found. If you are using child routes then this won't work unless you inject the child routers into your app.js (or wherever you define your routes) and calling configureRouter and passing in the main router's config. I found that this registered all the routes on the main router, although it seems really bad to me.
The problem you have is that sub routes tend to use child routers.
This enables some pretty powerful scenarios in Aurelia but presents the challenge that your route configuration might not be present until you have navigated to a child route.
I've handled this scenario before by proving a routing service that surfaces a routing tree as a single object with some helper methods to transform parts of this into a route config object which Aurelia can consume.
This is then injected into the sub modules and they query it to configure the router
The nav menu component can then look at this tree to build the menu structure ahead of any child modules being loaded.
Mike Graham also does a similar thing but he just sets all the route config up front (using a "level" variable on the route config to determine the menu heirarchy):
Aurelia: child router routes display in "main" nav-bar and child view in app.html <router-view> element?
The disadvantage to that approach is that you need to know about submodules ahead of time in order to configure the router. (part of the power of child routers is that they can just register at runtime and can be "dropped in" without any config anywhere else in the hosting app - this negates that advantage)
The disadvantage of the aforementioned approach is that you can't really generate route hrefs using the router easily (since it uses the parent to figure out what the relative href is) and you end up having to build the navmodel yourself.