How to set default activationStrategy in aurelia
Asked Answered
F

3

12

Aurelia normally ignores any changes in the querystring.

It is possible to set the activationStrategy to invoke-lifecycle in the VM so it will re-run the all the life cycles in the VM when the querystring changes.

To prevent littering my code (placing it in every VM) i want to set the default activationStrategy to invoke-lifecycle.

In the interface it's explained that it is possible, but how to set it? https://github.com/aurelia/router/blob/master/src/interfaces.js

Fitch answered 12/10, 2016 at 13:31 Comment(0)
G
28

On the ViewModel

(I misread your question at first too, but I'm leaving this in for completeness)

Place a method determineActivationStrategy() on the ViewModel and from there you can return the name or type of the activation strategy you wish to use. Example:

determineActivationStrategy() {
    return "invoke-lifecycle";
}

The strings "invoke-lifecycle" or "replace" will work. You can also use the typed version by importing the enum activationStrategy and returing activationStrategy.replace / activationStrategy.invokeLifecycle. They work the same.

In the RouteConfig

Or, as stated by Marton (who gave this answer before I did), you can put it directly in route config as the property activationStrategy.

This approach is better suited if the strategy does not depend on any particular state of your ViewModel and you don't wish to litter your view model with this stuff.

invoke-lifecycle vs. replace

In your question you say you want to

re-run the all the life cycles in the VM

Note that invoke-lifecycle reuses the existing ViewModel and will only invoke the router activation lifecycle, which is as follows:

  1. canDeactivate()
  2. deactivate()
  3. canActivate(params, routeConfig, navigationInstruction)
  4. activate(params, routeConfig, navigationInstruction)

Whereas replace will throw away the existing ViewModel and invoke the whole ViewModel lifecycle again on top of the router activation lifecycle:

  1. canDeactivate()
  2. deactivate()
  3. detached()
  4. unbind()
  5. (new instance): constructor()
  6. canActivate(params, routeConfig, navigationInstruction)
  7. activate(params, routeConfig, navigationInstruction)
  8. created(owningView, thisView)
  9. bind(bindingContext, overrideContext)
  10. attached()

So if you really want to run all the ViewModel lifecycle steps, you'll need to use replace.

Gilgai answered 12/10, 2016 at 16:3 Comment(3)
Using 'determineActivationStrategy' was my first thought as well. Then I realized that Vijay wants to avoid placing it into every VM. Your answer has more details, so +1! :)Jenson
Funny, I actually misread his question at first too and thought he wanted to place it on the ViewModel! Only now that you mentioned it and I re-read it, I spotted that. So your answer was actually correct before mine was. Upvoted as well :pGilgai
I talked to a aurelia dev and he said the same thing. Placing it in the route config doesn't work however. I will make a issue for it, thanks :)Fitch
J
11

activationStrategy is a property of RouterConfig, which represents the route config object used by config.map(). I think that you need to set it on each route definition.

Example:

configureRouter(config, router) {
  ...
  config.map([
    { 
      route: ['', 'home'],       
      name: 'home',       
      moduleId: 'home/index', 
      activationStrategy: 'invoke-lifecycle'
    }
  ]);
  ...
}

(Edit reason: I've made a terrible mistake by misreading your question at first, sorry :))

Jenson answered 12/10, 2016 at 14:43 Comment(1)
This does not seem to work w/Aurelia v1.0.0-rc.1.0.0.Misprision
G
1

You can use config.options.compareQueryParams = true.

Changelog entry

Gonick answered 19/10, 2018 at 17:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.