Angel Silvan
Angel Silvan

Reputation: 83

Re-executing onInit doesn't work

I have two views: Login and Detail.

When I log in and go to the second view, I want to print in console: console.log("Hello World").

I tried to write the console.log in the onInit function, but when I log out and relogin, the onInit function doesn't execute again. Does anyone know how to make it work?

I tried the next code too, but when i log out, the console.log executes too.

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

function sayHelloWorld(){
    console.log("HelloWorld");
}

Upvotes: 0

Views: 125

Answers (1)

matbtt
matbtt

Reputation: 4231

This works as expected. Method onInit is intended for one-time initializiations and is therefore only called once in the lifetime of a view. You could of course destroy our view when you logout but when you add a third view to application and navigate around this would again not work.

Best solution would be to attach your detail controller to a specific route in onInit (assuming name is "detail"):

this._oRouter.getRoute("detail").attachMatched(this.sayHelloWorld, this);

or, to check in the sayHelloWorld which route did match (assuming name is "detail"):

sayHelloWorld : function(event) {
    if (event.getParameter("name") === "detail") {
       jQuery.sap.log.info("HelloWorld");
    }
}

On remark: The code you posted looks strange. Are you sure that you implement the controller correctly. See documentation for more details.

Upvotes: 0

Related Questions