3

I'm using the hot towel template, and I'm trying to understand how to navigate to a different view via a javascript call. When my page loads, it looks like this:

enter image description here

Then, if I click any other button, then click the apps button again, I wrote some test code to just take the user to the ping page. This is in the apps view model:

function activate() {
    if (initialized) { router.navigateTo("#/ping"); return; }

    // more code here (doesn't get hit the second time through)
}

But what happens is the URL is correctly the ping URL, and the ping button is selected, but the actual content is still showing the applications:

enter image description here

If I want to navigate to another page without clicking in the navbar at the top, how should that be done?

Bob Horn
  • 33,387
  • 34
  • 113
  • 219

1 Answers1

2

Your 'router.navigateTo('#/ping') is correct.

But when activate method is called, lots of heavy tasks are being done by durandal, it's too late for

your commanding, if you want to prevent opening a page and instead of that You'd like to go to

another page , then you can use 'CanActivate' method as following :

function canActivate() {
    if (initialized) { router.navigateTo("#/ping"); return false; 
                                         /* return false to prevent opening a page */ }
    else return true;
}

Also your application's performance will be boosted too

Good luck.

Yaser Moradi
  • 3,267
  • 3
  • 24
  • 50
  • That did it. Thanks! I had to *add* the `canActivate()` method and I had to add this to my viewModel object literal: `canActivate: canActivate`. – Bob Horn Sep 08 '13 at 02:47