Let's say we have bootstrap 3 navbar, and part of the template could look like this:
<ul class="nav navbar-nav">
<li repeat.for="row of router.navigation" class="${row.isActive ? 'active' : ''}">
<a data-toggle="collapse" data-target="#skeleton-navigation-navbar-collapse.in" href.bind="row.href">${row.title}</a>
</li>
</ul>
This is also example from Aurelia docs.
Now let's say, I'd like to add one item with dropdown:
<ul class="nav navbar-nav">
<li repeat.for="row of router.navigation" class="${row.isActive ? 'active' : ''}">
<a data-toggle="collapse" data-target="#skeleton-navigation-navbar-collapse.in" href.bind="row.href">${row.title}</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button">Dropdown <span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Some page</a></li>
<li><a href="#">Some other page</a></li>
</ul>
</li>
</ul>
How can I configure routes for this dropdown? Do I need second router? Child router?
You don't have to use the
nav
propertyIn my experience, the
nav
property is at best a nice proof of concept. In practice, most applications have more complicated menus, and are better suited to more complex data structures behind them.Strategy 1: Hard code the menu
For not-too-complex situations, especially well defined applications that are unlikely to receive more, complicated routes, just hard code them into the view.
This strategy loses all of the fun, flashy Aurelia-enabled data-binding. But it might save you hours. It is simple and straightforward, and leaves no room for bugs. It's just HTML. The major downside as that this requires a little bit of additional coding for the
isActive
property, but its significantly easier to write this code than to try to write a dynamic, nested menu.Strategy 2: Create a custom menu model
For more complex, dynamic situations, especially when you have little idea what to expect at runtime, I recommend creating your own class or interface that describes your menu.
models/menuItem.ts
app.ts
This is quite a bit more complicated, but affords you a great deal of customizability and flexibility that the default router
navModel
does not.One option would be to create a custom settings property on the configuration of the router and then filter out the navigation items as needed. Note: This feels a bit hackish and there may be other alternatives, but this is what I could come up with.
configRouter method
Navigation Bar