14

I'm searching the mode to execute a code (in my case the retrieve of data to visualize from server) every time I view a page (every time the page is called by splitApp.toDetail or splitApp.backDetail). How can i do it?

P.S. The onBeforeRendering and onAfterRendering execute only the first time.

Mike
  • 14,010
  • 29
  • 101
  • 161
padibro
  • 1,324
  • 10
  • 56
  • 95
  • _> The `onBeforeRendering` and `onAfterRendering` execute only the first time_ --> See https://stackoverflow.com/a/55099341/5846045 – Boghyon Hoffmann Jun 10 '20 at 15:18

3 Answers3

15

There is a solution for you. There is a event called routeMatched when navigation is triggered every time. You can attach the event in the detail page.

 onInit : function () {
    this._oRouter = sap.ui.core.UIComponent.getRouterFor(this);
    this._oRouter.attachRouteMatched(this.handleRouteMatched, this);
},

handleRouteMatched : function (evt) {
    //Check whether is the detail page is matched.
    if (evt.getParameter("name") !== "detail") {
        return;
    //You code here to run every time when your detail page is called.
}
Mike
  • 14,010
  • 29
  • 101
  • 161
Haojie
  • 5,665
  • 1
  • 15
  • 14
7

I´m using onBeforeShow in my target views for that.

onBeforeShow : function(evt) {
    // gets called everytime the user 
    // navigates to this view
},

This is a function which is fired by a NavContainer on its children in case of navigation. It´s documented in the NavContainerChild.

Tim Gerlach
  • 3,390
  • 3
  • 20
  • 39
  • Did you insert `onBeforeShow` in your target view. Means in the view that is targeted in `splitApp.toDetail()`? – Tim Gerlach Aug 05 '14 at 14:36
  • yes, i replace `onBeforeRendering` with `onBeforeShow`. Now the code is never executed – padibro Aug 05 '14 at 14:40
  • Ok I tried it again and it works. In [this page](https://sapui5.netweaver.ondemand.com/sdk/docs/guide/5d13fd472e5b432b92848b7e46f6b648.html) it´s described to manually subscribe to this event. You could try this otherwise you can show us some more code (split app, views). – Tim Gerlach Aug 06 '14 at 07:45
  • but on your application runs without registers for `beforeShow` event? I only replace `onBeforeRendering` with `onBeforeShow`but not register a view by `this.addEventDelegate({ onBeforeShow: function(evt) {...` – padibro Aug 07 '14 at 09:13
  • Yes in my case it works without registering `onBeforeShow`. All my views simply provide `onBeforeShow` like `createContent`. [This thread](http://scn.sap.com/thread/3459136) on SCN is about events like `onBeforeShow`. Since you are using splitApp it should call `onBeforeShow` and related events for you. But let´s see some of your code to investigate it. You can simply edit and extend your question. – Tim Gerlach Aug 07 '14 at 09:23
  • 2
    I register into onInit... it work... `//INIT onInit: function() { //to register for beforeShow event this.getView().addEventDelegate({ // not added the controller as delegate to avoid controller functions with similar names as the events onBeforeShow : jQuery.proxy(function(evt) { this.onBeforeShow(evt); }, this) }); }, onBeforeShow : function(evt) { alert("onBeforeShow"); },` – padibro Aug 07 '14 at 09:24
  • Very interesting. I don´t know why you explicitly have to register for the events but glad to see that it works now. – Tim Gerlach Aug 07 '14 at 10:09
  • Is it possible to get url parameters when inside onBeforeShow method? – zygimantus Sep 17 '18 at 18:04
5

If routing is used, another version of Allen's code:

 onInit : function () {
   this._oRouter = sap.ui.core.UIComponent.getRouterFor(this);
   this._oRouter.getRoute("detail").attachMatched(this.handleRouteMatched, this);
},

  handleRouteMatched : function (evt) {
    //You code here to run every time when your detail page is called.
  }
danpop
  • 967
  • 13
  • 25