How to handle / ignore a bad route with durandal?

后端 未结 2 827
南旧
南旧 2021-01-03 10:43

I\'m trying to conditionally block a route from being accessed. I think this can be done with guardRoute: http://durandaljs.com/documentation/Router/

2条回答
  •  旧时难觅i
    2021-01-03 10:51

    I am a newbie to durandaljs 2.0 having only used it for a couple of weeks so this was one of my first stumbling blocks. see my code below

    router.guardRoute = function (routeInfo, params, instance) {
        var insecureRoutes = ['login', 'terms','signup','password'];
        if ($.inArray(params.fragment, insecureRoutes) || mymodule.isAuthenticated()) {
            return true;
        } else {
            return 'login/' + params.fragment;
        }
    };
    

    I also define a route for the login page like this

    { route: 'login/(:returnroute)', moduleId: 'viewmodels/login', nav: false },
    

    This allows you to optionally specify a return rout so that after login you can redirect the user to where ever it is they were initially trying to go to. I hope it helps someone

提交回复
热议问题