0

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

It 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?

Chessinio
  • 11
  • 1
  • 6
  • In my work examples I use this.getModel("ModelName"); and it works fine. Have you tried to maybe name your model before trying to get it? – wdoering Dec 13 '16 at 12:03
  • Yes, I have tried naming the model. Does not make any difference. Are you calling the model from inside onInit ? – Chessinio Dec 13 '16 at 12:52
  • About models being undefined: https://stackoverflow.com/a/42251431/5846045 About retrieving OData: https://stackoverflow.com/a/46662969/5846045 Pretty much what [matbtt's answer](https://stackoverflow.com/a/41120840/5846045) already mentioned. I'd suggest to accept his answer to inform others that this question got solved. – Boghyon Hoffmann Oct 25 '17 at 21:43

1 Answers1

0

This works as expected. Please check the documentation about application initialization.

I assume that this is related to the way model propagation is working. A model instantiated by the component is propagated to its views unless the specific view has a model with the same name. Normally you instantiate view specific models in onInit, i.e. the runtime knows all view specific models only after onInit has been performed.

Normally you should be able to get the component models in onInit via:

this.getOwnerComponent().getModel()

Update: As far as I know getProperty does not trigger a read automatically. Thus you have to to do it own your own (documentation):

this.getOwnerComponent().getModel().read(path, paramaters);

With parameters you pass a success handler where you can access the required values.

matbtt
  • 4,230
  • 19
  • 26
  • Good to have it confirmed! But it should be possible to use the model from onInit(). Do you know how to do this? Any workarounds? – Chessinio Dec 13 '16 at 12:55
  • Updated my answer. Does this.getOwnerComponent().getModel() work? – matbtt Dec 13 '16 at 13:51
  • this.getOwnerComponent().getModel() returns the correct odata model object, but is not filled with any data yet. It returns an empty model. Seems like the model gets populated after onInit(). The model is instantiated in manifest.json. – Chessinio Dec 13 '16 at 14:11
  • Which data? You need to bind an control, e.g. table to trigger a data read or call the read by yourself. Normally you do the first. – matbtt Dec 13 '16 at 14:20
  • I need to store some properties from the model into some local variables, like this: var oModel = this.getOwnerComponent().getModel(); var value = oModel.getProperty("/ZcountWerksSet/Werks"); // value return "undefined" because the model is empty at this point. Do I have to bind it to a control, or can I do it like this? – Chessinio Dec 13 '16 at 20:15
  • Please extend the question or open a new one. Excessive usage of comments is not appreciated and Stackoverflow will suggest to go into chat mode. – matbtt Dec 13 '16 at 20:36
  • Sorry. Question extended. – Chessinio Dec 14 '16 at 07:26