Question

I have a two routes with different hashes and same view model:

var routes = [
    { route: '', moduleId: 'home', title: 'Home', nav: 1 },
    { route: 'details(/:id)', moduleId: 'details', title: 'Details', nav: 2, hash: '#details'  },
    { route: 'access_token=*token', moduleId: 'details', title: 'Details', nav: false, hash: "#access_token=" }];

How I can detect the way I came to details view model in activate method of details view model? I tried to iterate over router.routes array and find route with isActive() == true, but this doesn't available till activate method returns result.

Also, if I add detecting of active route into binding method of view model, I getting both routes active, regardless of which route was applied:

function binding() {

    router.routes.forEach(function (route) {
        console.log('Route ' + route.hash + " isActive:" + route.isActive());
    });
}

Console log:

Route # isActive:false details.js:37
Route #details isActive:true details.js:37
Route #access_token= isActive:true 
Was it helpful?

Solution

Take a look at the router's activeInstruction property -

router.activeInstruction();

You can subscribe to it or create an computed off of it to track changes in the route.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top