The redirect flow events

Understand about the redirect flow and how to handle it's events

Redirecting is changing the route you're currently in. Hi-Framework is based on the MVC pattern, what means that route endpoints are controller actions and actions themselves are responsible for calling the view to be displayed.

In the diagram below, we represent the whole redirecting flow, from client to server-side and back to client-side.

onRedirectStart

The redirect process will mostly begin on the client-side when the end-user clicks on an element with the ajaxify attribute or in some way invokes the Hi.redirect method.

When the act of redirecting begins, the method $onRedirectStart on the template controller is fired.

	$onRedirectStart : function(route){

            //Do something

	}

TIP
Use this event to display your redirecting loader. You will later have to hide it when the redirect process ends.

NOTICE
The $onRedirectStart method receives one argument which is the route to which the user is being redirected.

ControllerRequestEvent

This event is what allows you to know every time a user accesses an action. This event will be fired twice in order to track two different moments : before and after the action method execution.

Before action execution

To track this moment, you just have to make sure the boolean in isAfter() is false.

 public void preAction(@Observes ControllerRequestEvent args) {
       
       
        if(!args.isAfter())
            //This is before the action method is invoked
            
            
}

TIP
Use this event while developing your app to willingly make it slower using Thread.sleep. It will help you simulate a bad response time scenario.

After action execution

To track this moment, you just have to make sure the boolean in isAfter() is true.

 public void postAction(@Observes ControllerRequestEvent args) {
       
       
        if(args.isAfter())
            //This is after the action method is invoked
            
            
}

onRedirectFinish

This event is fired at the end of the redirect flow. To be specific, in cases of successful redirects, it will be called right after the view's postLoad. In cases of errors, this event will be called after onRedirectError.


$onRedirectFinish  : function(route){


}

TIP
If you have displayed a loader on the start of the redirect, you should hide it here.

onRedirectError

In cases of errors in your redirect flow, your redirect will be interrupted and this method is going to be called.

	
//Handles HTTP Errors on redirect
$onRedirectError : function(route,httpErrorCode){
		
		

}

Github Repository