The guardRoute function

24 views
Skip to first unread message

Lazar Gubezkis

unread,
Jul 12, 2015, 4:38:55 AM7/12/15
to duran...@googlegroups.com
I decided to test the guardRoute function, as I plan to use it for user authentication: This is my activate function at shell.js:

            router.map([
                { route: '', moduleId: 'viewmodels/main', nav: true }
            ]).buildNavigationModel();

            router.guardRoute = function (instance, instruction) {
                console.log('Your route is: ' + instruction.config.route);

                return instruction.config.route;
            }
                        
            return router.activate();

When accessing my route, the message "Your route is: " was printed to console, followed by a message "Navigation Redirecting". Then the page got stuck, displaying only the splash loader. What did I do wrong?

Thanks.
Message has been deleted

Garry Taylor

unread,
Jul 12, 2015, 6:21:44 AM7/12/15
to duran...@googlegroups.com
There are two things here.

1) You should make your Guard Route before the standard mapping.
2) You should return true, false or a url to redirect

Because you are returning the config.route (String) this will be noted as a URL and redirect, then hit the guardroute and redirect again then hit the guardroute and redirect then ... think you get the point :)

Try this:
You need to add the config to the Guard Route before the general route config.

            router.guardRoute = function (instance, instruction) {
                console.log('Your route is: ' + instruction.config.route);
                return true;
            }

            router.map([
                { route: '', moduleId: 'viewmodels/main', nav: true }
            ]).buildNavigationModel();
                        
            return router.activate();

Reply all
Reply to author
Forward
0 new messages