• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

I Like Kill Nerds

The blog of Australian Front End / Aurelia Javascript Developer & brewing aficionado Dwayne Charrington // Aurelia.io Core Team member.

  • Home
  • Aurelia 2
  • Aurelia 1
  • About
  • Aurelia 2 Consulting/Freelance Work

How To Get The Current Active Route In An Aurelia ViewModel

Aurelia 1 · February 21, 2016

When inside of an Aurelia ViewModel there are situations where you might want to know what the current route is within a ViewModel.

One such scenario I encountered recently where I had one View and ViewModel pair being accessed by 5 different routes. I needed to know what route was accessing my ViewModel to load different data.

This is what the second parameter of the activate method for.

activate(params, navigationInstruction) {
    console.log(navigationInstruction);
}

The navigationInstruction parameter gives you the information about the route currently accessing the ViewModel. This allows you to access everything about the route, including the navModel

This object has a tonne of information about the currently active route accessing this ViewModel:

  • moduleId – The current module path to the ViewModel (defined in app.js/app.ts)
  • name – The name of your route (defined in app.js/app.ts)
  • navModel – This pretty much gives you the same values as the other properties with exception of a couple, like isActive and relativeHref
  • route – The route pattern (defined in app.js/app.ts)
  • settings – The settings object defined in the route (defined in app.js/app.ts)
  • title – The title value for this route
  • viewPorts – An object of defined viewports for this route (defined in app.js/app.ts)

In my scenario, I needed to know the name of the route accessing the ViewModel. I knew if it was my route for numeric pagination, I could check for specific parameters. If it was the route with no parameters, I knew not to check for them and just fetch all data.

Hopefully this helps you in determining the current route in Aurelia and what is accessing your ViewModel.

Dwayne

Leave a Reply Cancel reply

3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
proggrockcode
proggrockcode
6 years ago

Exactly what I was looking for – thanks for this!

0
Erik Vullings
Erik Vullings
6 years ago

Thanks for the tip!

Although `routeConfig.navModel.href` gets me the active url, this only seems to work when the application is already loaded and I’m actually navigating from one url to another, i.e. not when I reload the application. In that case, the navModel is still empty (at least in Aurelia v1).

Any thoughts?

0
Josh McCullough
Josh McCullough
5 years ago

I find that on the initial load, there is only one argument passed to activate(), and it’s undefined. Any ideas?

1

Primary Sidebar

Popular

  • Thoughts on the Flipper Zero
  • Testing Event Listeners In Jest (Without Using A Library)
  • How To Get The Hash of A File In Node.js
  • How To Paginate An Array In Javascript
  • Waiting for an Element to Exist With JavaScript
  • Reliably waiting for network responses in Playwright
  • How To Get Last 4 Digits of A Credit Card Number in Javascript
  • Neural DSP Reveal Details About the Long-Awaited Quad Cortex Desktop Editor
  • How to Use Neural DSP Archetype Plugins With the Quad Cortex
  • How To Install Eufy Security Cameras Without Drilling or Using Screws

Recent Comments

  • Kevmeister68 on Start-Ups and Companies That Embrace Work From Anywhere Will Be More Likely to Survive the Coming Recession in 2023
  • kevmeister68 on What Would Get People Back Into the Office?
  • Dwayne on PHP Will Not Die
  • Dwayne on How to Create a Blockchain With TypeScript
  • kevmeister68 on PHP Will Not Die

Copyright © 2023 · Dwayne Charrington · Log in

wpDiscuz