Navigation Events

SAPUI5 Route Events

One of the common problems in SAPUI5 is all about routes. Today will look into 2 different route events of SAPUI5.

Before we start, we need to understand how navigation events happen in SAPUI5. Let’s take an example below:

Navigation events in SAPUI5 are handled by Router classes. Some known router classes are sap.ui.core.routing and sap.m.routing.

Router classes will find patterns maintained in the manifest.json and try to match those patterns into routes.

Routes now contain which exact files are to be rendered or displayed on the screen, these files are called targets.

The router will then pass the pattern and display information to the view for the creation and caching.

Events on Route

There are two commonly used events on route matching, these are attachRouteMatched and attachMatched.

attachRouteMatched

This event is triggered for any pattern that matches the routing configuration from manifest.json.

You would ask,

why on earth I would listen to all route events? isn’t that’s too much?

As an example, this is good for creating analytics for your application. You can add an event handler and log the usage of pages of your app and send them to the back-end as a report.

attachMatched

Now, what if you want to listen to a specific route instead? For example, you only want to handle events for your home view, this is where attachedMatched is used.

oEvent from above will contain the route event parameters.

Router Event Parameter
Router Event Parameter

Event Parameters

You have the following details from the attachedMatched event:

Parameter Remarks Example
name Name of the route that matched Home
arguments Parameters of the URL hash “?query”: {“EndDate”: “2019-11-21”, “StartDate”: “2019-11-08”}
config Configuration of the route from manifest.json “config”: {“routerClass”: “sap.m.routing.Router”, “viewType”: “XML”, “async”: true }

Conclusion

In conclusion, it is very important to know when to use attachRouteMatched and attachMatched. Utilizing which route event handler will significantly improve your application as well.

Remember that attachRouteMatched will be triggered for any route pattern, which will cause overhead on route checking if not handled properly.

If you like this post, don’t forget to drop a comment, like or share. Also, there’s a subscription button you can use for more SAPUI5 tutorials.

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.