Skip to content

Instantly share code, notes, and snippets.

@cschuff
Created February 8, 2017 08:36
Show Gist options
  • Save cschuff/e953450fa42650aee78fbeb7939ccdce to your computer and use it in GitHub Desktop.
Save cschuff/e953450fa42650aee78fbeb7939ccdce to your computer and use it in GitHub Desktop.
Auto-attach to patternMatched event on correct sap.ui.core.routing.Route
////////////////////////////////
// Controller Lifecycle
onInit: function () {
this.getRouter().attachRoutePatternMatched(this.onAnyRoutePatternMatched, this);
},
onExit: function () {
this.getRouter().detachRoutePatternMatched(this.onAnyRoutePatternMatched, this);
this.getRouter().getRoute(this.routeName).detachPatternMatched(this.onRoutePatternMatched, this);
},
////////////////////////////////////////////////////////////
// Navigation
onAnyRoutePatternMatched: function (oEv) {
// store route name when first matched
if (!this.routeName) {
this.routeName = oEv.getParameter("name");
// attach pattern matched now that we now this views route name
this.getRouter().getRoute(this.routeName).attachPatternMatched(this.onRoutePatternMatched, this);
// manually trigger attachPatternMatched since we already missed the first event here
this.onRoutePatternMatched(oEv)
}
// view is left, clean up here
if (oEv.getParameter("name") !== this.routeName) {
this.getView().unbindElement();
}
},
onRoutePatternMatched: function (oEv) {
// do reoccuring view init here (treat like onBeforeShow)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment