How to run routing pipeline manually in proper way?


#1

Hi,

For quite long I struggle with problem of running navigation events on all current view ports properly. I use some dirty hacks but I feel it’s wrong and error prone.

To be specific. I would like on certain actions if all attached view-models can be deactivated WITHOUT doing any real navigation. That is, I would like to call all canDeactivate() methods and check if any would return false.

Could you please direct me in the right way?


#2

I’ve had to do something similar, when a core data point changed we wanted to refresh the data on each vm of our current route in order, EX: root => vm 1 => vm 2 => vm 3

My hacky implementation was putting a random char at the beginning of the route and changing it by splicing apart window.location.hash and resetting it since the router system reacts whenever that string was changed.

Doing so would trigger the activate() method chain down the line from root to vm 3 in order on each view model. If you return a promise in the activate it’ll also wait until it’s finished to move onto the next vm. I’m guessing it would also trigger the deactivate and canDeactivate methods as well.

Not sure if that helps you out.

I’d be curious to know if anyone has found a better way aside from some sort of priority event system.


#3

Thanks for reply and sharing expirience.
That would probably do the trick, but what worries me is that it actually performs navigation action. What I’m trying to achieve is to run canDeactivate() events only. Without any real action.


#4

Ah mine doesn’t navigate, it only triggered activate() and deactivate() the attached() and detached() were never fired.

Though it is implemented in an unfortunately complicated way.

Should be noted that this is with the hash # navigation which is default for Aurelia and not pushstate.


#5

I have finally found way around that really suits our need. I have created
empty view-model and in its attached I’ve put action that should be
triggered. Then I just perform navigation to it. In effect all existing
canDeactivate/deactivate methods are called as always. I don’t believe that
I have not found this solution earlier :wink:


#6

if you set activationStrategy: activationStrategy.invokeLifecycle, for the route you want, activate will be called every time the route changes. Is this the issue you’re trying to solve?