cancel
Showing results for 
Search instead for 
Did you mean: 

SAPUI5 - Retrieve OData model object from controllers onInit() function

pearlalex
Explorer

I'm trying to retrieve an OData model from inside onInit function from a controller in SAPUI5 for storing a property into a local variable. I have tried the following without success:

var oModel = this.getView().getModel();// returns undefined
var oModel = sap.ui.getCore().getModel();// returns undefined

I have googled the problem, and it seems like the model is not yet set when the onInit() is running. Because of this, I have tried:

var oModel = this.getOwnerComponent().getModel(); // returns empty odata object
var value = oModel.getProperty("/Collection/Property"); // returns undefined

The exact same code works just fine in all other functions except onInit(), but I need it inside this function.

How can I retrieve the model and property in onInit?

Accepted Solutions (0)

Answers (2)

Answers (2)

SergioG_TX
Active Contributor
0 Kudos

I think the onBeforeRendering should only run the first time the view is loaded... otherwise, you can check if the model is instantiated or set a property after it is instantiated so you can skip the validation on subsequent runs ,... maybe this would work

SergioG_TX
Active Contributor
0 Kudos

would it meet your requirement if you add it on the onBeforeRendering() event instead of the onInit() event?

your model will be ready from there onwards

pearlalex
Explorer
0 Kudos

The model is loaded when onBeforeRendering() runs and the property can be retrieved. The only problem is that this function is triggered on every screen change or even when a dialog appear, so it's not the best solution in my case. I need it to run once at startup.