durandal-2.0durandal-navigation

How to access the previous route from Durandal router


Is there a way to access the previous route/instruction from the Durandal.js router? I know I can do router.navigateBack() to actually navigate back, but I want to know the route that I will navigating back to before I trigger the navigation.


Solution

  • Not directly I'm afraid.

    router.navigateBack() is defined in router.js as

        router.navigateBack = function() {
            history.navigateBack();
        };
    

    And history.navigateBack() is defined in history.js as

        history.navigateBack = function() {
            history.history.back();
        };
    

    and history.history is an alias defined above for window.history which is part of the browser and would cause security issues if exposed.

    If you want to have that functionality you'll have to implement it yourself. Possibly by intercepting router.navigate() and router.navigateBack().