RobVious
RobVious

Reputation: 12915

Durandal: Determining if a view is currently active (possibly using Router)?

My shell has buttons that should be displayed only if the user is currently viewing a certain viewModel. How can I determine whether or not a view is currently "active" from the shell?

Upvotes: 3

Views: 3233

Answers (1)

RainerAtSpirit
RainerAtSpirit

Reputation: 3723

Assuming that the routes are defined in shell.js activate then using viewAttached will allow access to the router object.

return {
    router: router,
    viewAttached: function () {
        console.log('activeRoute', router.activeRoute());
    },
    activate: function () {
        router.map([...

Update based on comment

In Durandal 2.x activeRoute() isn't defined any longer. Use router.activeInstruction() instead.

Update 2 based on comment

In Durandal 2.x viewAttached is now attached.

Note: For those who need access to the current route's configuration in order to get at custom properties or other information, you can access the raw data via router.activeInstruction() The instruction contains the fragment, queryString, params, and queryParams. It also contains a config property which is the same instance as your original route config.

http://durandaljs.com/documentation/Conversion-Guide/

Upvotes: 8

Related Questions