Not as simple as it seems, though it's still possible w/o any 3rd party solutions.
Let's assume that the routes (in manifest.json) are:
"routes": [
{
"pattern": "",
"name": "Main",
"view": "main",
"targetAggregation": "pages"
},
{
"pattern": "create",
"name": "Create",
"view": "myView",
"targetAggregation": "pages"
},
{
"pattern": "edit/{id}",
"name": "Edit",
"view": "myView",
"targetAggregation": "pages"
},
{
"pattern": "view/{id}",
"name": "View",
"view": "myView",
"targetAggregation": "pages"
}
]
So in this case you would like to get the current name: Create, Edit or View.
note 1: In my case there's one view/controller ("myView") for handling three different types of displays, but it doesn't matter - you can
have separate views/controllers as well. (see note 2 for more details).
So here's what you have to do in order to retrieve those route names:
Step 1:
Create a global model in you manifest.json
"models": {
"applicationModel": {
"type": "sap.ui.model.json.JSONModel",
"settings": {},
},
}
Step 2:
In the given controller (myView), use the routers attachRouteMatched method in the built-in onInit method:
onInit: function(){
var oRouter = sap.ui.core.UIComponent.getRouterFor(this);
oRouter.attachRouteMatched(this.routeMatched, this);
}
note 2: If you have a multi-page application, set the event handler in your root controller or component.js. These two are always loaded, no matter which subpage you are on.
Step 3:
In the same controller, mostly directly under the onInit method, create the callback method (in my case it's called routeMatched):
routeMatched: function(oEvent){
var oParameters = oEvent.getParameters(),
var sRouteName = oParameters.name; // Yay! Our route name!
var oModel = this.getView().getModel("applicationModel");
oModel.setProperty("/routeName", sRouteName);
}
Step 4
Now wherever you have access to your model, you simply call it's new property: routeName.
this.getView().getModel("applicationModel").getProperty("/routeName");