0

I'm struggling to find a way to execute a function in the main controller when it loads. When the main controller is loaded the first time, I can get that function executed inside onInit. But the issue is when user logs out and logs back in the main controller, the method onInit does not get executed again. Is there a way to execute a function every time when controller loads?

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
user557657
  • 856
  • 1
  • 12
  • 35
  • Does this answer your question? ["onBeforeRendering" or "onAfterRendering" is not called every time the view is opened](https://stackoverflow.com/questions/55082731/onbeforerendering-or-onafterrendering-is-not-called-every-time-the-view-is-o) – Boghyon Hoffmann Jul 13 '21 at 15:08
  • The above linked Q&A talks about `onB(efore|After)Rendering` but it's the same issue with the same solution – Boghyon Hoffmann Jul 13 '21 at 15:09

2 Answers2

2

Below code will help you to achieve what your are looking for

onInit: function() {
  this.getRouter().getRoute("routeName").attachPatternMatched(this._onObjectMatched, this);
}

_onObjectMatched: function() {
  //this function executes every time you navigate to this page
}

Demokit link for detailed information

santhosh
  • 463
  • 4
  • 15
0

What do you mean by "when user logout and log back in the Main Controller"?? When your app loads the view the first time, it executes the onInit, onBeforeRendering, onAfterRendering and other lifecycle events. But if you don't destroy the controller instance you never 'log out' of it. It remains there as an object in your DOM and their functions can be called whenever is needed.

Now, if you are using the UI5 Router to navigate back and forth to other views, then I suggest you to set 'PatternMatched' events in your router. This events will be fired whenever the given pattern is match, no matter if it is the 1st or the n-th time.

Check out:

Rafael López Martínez
  • 2,225
  • 2
  • 15
  • 23