On a per route basis you might want to configure some additional route specific data that you can access in your view. For example a route might have an icon for your navigation menu. This actually came up in the Aurelia Gitter chatroom and I decided to do a quick little write up.
Say hello to a little unknown property called settings
Added all the way back in February 2015 when Aurelia was still a tiny blip on the Javascript radar was this property which allows you to define an object of additional properties for a route, similar to how Durandal does it.
Defining a basic route
Nothing out of the ordinary here except for the settings object being passed as an additional parameter on our defined route.
configureRouter(config, router) {
config.title = 'Test Route';
config.options.pushState = true;
config.map([
{
route: ['', 'welcome'],
name: 'welcome',
moduleId: './welcome',
title: 'Welcome',
settings: {
icon: '/images/icons/welcome-icon.svg'
}
}
]);
this.router = router;
}
Accessing the settings data
Taking part of the nav-bar.html
partial from the Aurelia Skeleton Navigation app, we can now access our additional values passed in through the predefined route.
<ul class="nav navbar-nav">
<li repeat.for="row of router.navigation" class="${row.isActive ? 'active' : ''}">
<img src="${row.settings.icon}">${row.title}
</li>
</ul>
It is worth noting that you do not have to use the settings param, you can just add them inline instead. However, this means if in future the Aurelia team add new configuration options to a route and they pick the same name as you, it will cause some headaches. The settings object namespaces your values and ensures they’re always there.
but how can I access those settings in my module .js file
ah, adding parameters (b,c,d,e,f,g) to the activate call I see (after changing the names to be more indicative)
activate( urlParams, routeMap, navInstr )
thanks for your blog, I’ve been reading it — and bought you book, that’s good too!
(I wish I could figure out how to search the API to find the function signature for activate() among others…)
@Allen
Yep. As you discovered settings are available via the activate method. The second parameter should give you what you want. I’ll update the post to reflect accessing the object.
Hey Dwayne,
I tried to show my help button in the right side of the nav-bar using settings but its not working
{ route: ‘help’, name: ‘help’, moduleId: ‘./help’, layoutView: ‘app/layout-toolbar.html’, nav: false, title: ‘Help’ settings:{ isRightNav:true }}
${row.title}
Thanks Dwayne, is there a way to pass arbitrary values?
For example, say I’m creating a selection menu and I’m looping through a collection of objects to generate links, can I include the object Id in routeConfig or navModel such that it doesn’t show in the browser’s address bar? I’ve tried `settings.bind` in the link but the values aren’t changed.
Thanks :o)