I have my Durandal routes configured as below.
var routes = [
....... More Routes Here.....
{
url: 'login',
moduleId: 'viewmodels/login',
name: 'Log In',
visible: true,
caption: 'Log In'
}, {
url: 'logout',
moduleId: 'viewmodels/logout',
name: 'Log Out',
visible: false,
caption: 'Log Out'
}, {
url: 'register',
moduleId: 'viewmodels/register',
name: 'Register',
visible: false,
caption: 'Register'
}];
And everything is working as expected. I would like to be able to activate the Logout Route in my navigation when I log in and my log in button to become invisible. I have tried the following code and despite not throwing any errors it does not change the visibility of anything in the interface.
var isLoggedIn = ko.observable(false);
isLoggedIn.subscribe(function (newValue) {
var routes = router.allRoutes();
if (newValue == true) {
for (var k = 0; k < routes.length; k++) {
if (routes[k].url == 'logout') {
routes[k].visible = true;
}
if (routes[k].url == 'login') {
routes[k].visible = false;
}
}
} else {
for (var i = 0; i < routes.length; i++) {
if (routes[i].url == 'logout') {
routes[i].visible = false;
}
if (routes[i].url == 'login') {
routes[i].visible = true;
}
}
}
});
I believe this doesn't work because visible is not an observable, isActive is a computed with no write capability so it does not work either. How can I dynamically change the visibility of my routes in the nav menu?