Durandal: How to route away from current view within that view's activate() function?

大兔子大兔子 提交于 2019-12-03 08:55:55

Here's @EisenbergEffect answer to a quite similar discussion in google groups.

Implement canActivate on your view model. Return a promise of false, then chain with a redirect.

You might want to give @JosepfGabriel's example (discussion) a try in Durandal 1.2. Check the correct router syntax for your Durandal version, you might have to substitute it with something like router.navigateTo("#/YourHash", 'replace').

canActivate: function () {
    return system.defer(function (dfd) {
        //if step 2 has a problem
        dfd.resolve(false);
    })
    .promise()
    .then(function () { router.navigate("wizard/step1", { trigger: true, replace: true }); });
}

However this is NOT working in Durandal 2.0 and there's a feature request https://github.com/BlueSpire/Durandal/issues/203 for it.

Autosonic

The following worked for me in Durandal 2.0:

    canActivate: function() {
        if(condition)
            return {redirect: 'otherRoute'};
        return true;
    }

    activate: // Do your stuff

It's mentioned in the documentation: http://durandaljs.com/documentation/Using-The-Router.html

You can't call redirect into the active method.

You can override the guardRoute method from router, to implement redirections.

You can do somehting like that:

router.guardRoute= function(routeInfo, params, instance){
  if(someConditios){
    return '#/someRoute'
  }
}

You can return a promise, true, false, the route to redirect... You can find more information about that in the next link: http://durandaljs.com/documentation/Router/

Rainer's answer was pretty good and works for me adding this small fix.

Inside the then() block simply call the navigation like this

setTimeout(function() { router.navigateTo('#/YOUR DESTINATION'); }, 200);

that should fix your problem. The setTimeout does the trick. Without it the newly navigated page catches the old NavigationCancel from the previous one.

Adding a return in your if (true) block should fix this.

function activate(routeData) {
    if (true){
       router.navigateTo("#/someRoute");
       return;
     }
    alert ("should not be shown");
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!