Component Lifecycle activate()

I’ve been using this activate() method in my components since I started using Aurelia (I originally came from Durandal) and to this day I have no idea what it does in Aurelia. It’s not even in the docs yet it executes right after a component’s constructor. Anyone have any ideas about this? Should I even use it? Why does it work if it’s not in the docs?

2 Likes

This might help

2 Likes

There are basically two lifecycles in Aurelia. One lifecycle is the component lifecycle. This includes constructor, created, bind, attached, detached and unbind. The second lifecycle is the router lifecycle. It includes canActivate, activate, canDeactivate, and deactivate. However, there’s a special case, which is that anything that is created through the composition engine (via compose element, application root, or manual use of the composition API) will have an activate callback invoked, if present.

We fully acknowledge that the 3rd special case is a bit confusing. We’re working to make this more consistent in vNext.

Let me know if that helps to clarify things and if there are any other questions related to this that we can answer.

7 Likes

Yep that pretty much explains it. Thank you for being such an active member of the community.

2 Likes

You are certainly welcome. I’m looking for more ways to focus on helping people here and improving our documentation. It means trading different responsibilities around on the team to free me up for more of that. So, takes a bit of time, but we’re working on it, so hopefully I’ll be hanging out around here more :slight_smile:

2 Likes

I was directed here after being surprised by activate() being called on my root component, but without any arguments. Here’s a conversation I had about this with @bigopon over in Gitter (slightly edited for brevity):

Jussi Mattila @jussimattila 13:59
I’m building an error page using aurelia.setRoot("error-page"). Why is that component not getting any params in activate()? activate() is called but the arguments are all undefined. I have to put a <router-view> on the error-page and another component inside that, which will see the params in its activate(params).

bigopon @bigopon 14:17
it’s because you activated it with composition engine via setRoot()

Jussi Mattila @jussimattila 14:18
ahh, so that is the third special case

bigopon @bigopon 14:18
yes. the reason it has no parameter in activate() is setRoot() doesn’t pass a model to composition engine in that 3rd special case, unlike aurelia-dialog

Jussi Mattila @jussimattila 14:19
I actually thought that setRoot() is part of routing. or more precisely, component instantiated by setRoot() would be part of routing lifecycle

bigopon @bigopon 14:20
if you look at <router-view/>, it’s just some custom element offered by the aurelia team to integrate completely separate worlds: templating + routing

Jussi Mattila @jussimattila 14:21
right. I have been bit by the missing <router-view> every now and then. need to have that to have features “injected” in the component

Hope this makes it even clearer!

2 Likes

Hi Rob,

Thanks for clearing that up – it has been a long-standing point of confusion for me. However, it does beg the question – what kind of code should be placed in activate vs bind or attached and which should be preferred for things like loading data for the view, etc.?

“active member” … uh, he basically invented this whole thing lol