History management
Problem description
In applications developed, history management is always different. You often need to manipulate history to remove previous route, as for example :
- During FTU, you should be able to navigate through steps, but once FTU complete, you should not be able to go back to FTU
- When navigating deeply between program info and related, you may need to skip some steps when pressing back
- โฆ
Moreover, when routing back to a screen you sometimes want to not redo everything (data loading, data connectionโฆ), because you just want to go back to the latest state. Currently in that case, you are adding a flag in the context.
Finally, it is observed that current implementation in RouterManager save routes till infinity. Everything is stored when using route methods, but it is never cleared. This could lead to memory issues.
To sum up needs:
- You need to easily override back behavior.
- You need to identify back navigation.
- You want to avoid memory leak on history management.
Proposed solution
Usage of routeToPrevious
methods on RouterManager
Using routeToPrevious
method will allow you to use mixing MHistoryRouter
that is responsible for history management. It will automatically add two options to the context:
skipHistory
: true to prevent add of this route to historyisBack
: true to identify back navigation if needed
By default, with usage of AbstractMHistoryRouter
, you will be redirected to previous route.
Caution
It is possible to deactivate history management by using disableHistory
flag on RouterManager
. If something is not working, please check that disableHistory
is not activated.
As you can send options to route, you can send options to routeToPrevious
that will be used to call route with previous route name. Those options will override potential stored options used for the first routing to this routeName.
Create implementation of MHistoryRouter
If you need a more complex implementation of previous behavior, you will need to create your own implementation of MHistoryRouterthat
which inherits from AbstractMHistoryRouter
. Then, you will need to override _getPreviousRoute
that is allowed to manipulate _routesHistory
stored.
Default implementation is the following :
_getPreviousRoute: function () {
this._routesHistory.pop();
return this._routesHistory.slice(-1)[0];
}
By using condition here, you can doโฆ whatever you want and finally return an element of the _routesHistory.
Conclusion
By using routeToPrevious
you will :
- always have an identifier for back navigation (isBack)
- be able to implement `_getPreviousRoute`` to have specific behavior
- prevent memory leak by having an entry point to manage stored history