The documentation outlines the sequence in which transition lifecycle hooks are executed as follows:
onBefore
onStart/onInvalid
onEnter (for individual states)
onSuccess
onError
However, there seems to be some outdated information within this description. Specifically, it highlights that onEnter
hooks pertain to entering a state, while onStart
hooks relate to commencing a Transition between states.
This distinction is expanded upon in the hook documentation pages:
onStart
hooks are asynchronously invoked, based on priority order, when the
Transition initiates. At this juncture, the Transition has neither exited nor entered any states.
onEnter
hooks are also asynchronously invoked, following priority order, during
the process of entering a state. The entry into states occurs after the
onRetain
hooks have been executed.
One potential use for an onStart
hook could involve transition validation, such as verifying user authentication. Here's an example snippet provided in the documentation:
$transitions.onStart( { to: 'auth.*' }, function(MyAuthService, $state) {
// Check if the user is authenticated
if (!MyAuthService.isAuthenticated()) {
// Return a promise for successful login.
// The transition will wait for this promise to resolve
return MyAuthService.authenticate().catch(function() {
// Redirect to a non-authenticated state
return $state.target("guest");
});
}
});
Based on the latest findings, the correct order appears to be:
onBefore - Transition initialization; additional hooks can be registered here dynamically
onStart - Transition begins execution
onExit - Transition exits a state
onRetain - Transition maintains a state
onEnter - Transition enters a state
onFinish - Transition completion stage, all states have been entered and exited
onSuccess/onError - Transition finalization (successful or unsuccessful)
Note: All hooks except the last two possess the ability to alter the transition by modifying the target state and so forth. onSuccess
and onError
occur after the fact: once the transition concludes.