question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Component + default OData model: this.getView().getModel() returns undefined in onInit() of controllers

See original GitHub issue

SAPUI Version: 1.30.0, 1.30.11, 1.32.0, 1.32.9, 1.34.2 Browsers: all Defect: Calling this.getView().getModel() in onInit() to get the default model in any controller of a component based app returns undefined. Expected: this.getView().getModel() should return the default model (v2.ODataModel) as instantiated via app descriptor. In other words: this.getView().getModel() should have the same result as calling this.getOwnerComponent().getModel() in onInit()

To reproduce you can simply use any of the tutorials, i.e. the Walkthrough, Navigation& Routing, or any other tutorial. Let’s try it with the Worklist tutorial:

As you can see, while the model is already on the component it is not visible in the onInit() of the controller.

Also, try the following in the onInit() while you are in the debugger (just execute in console):

window.controller = this;

Then, after the onInit() has finished call on the console:

controller.getView().getModel();

This shows that the model is visible somewhen later, after onInit() has finished. So it seems there is a timing problem (the model is propagated too late). In the route matched handlers this.getView().getModel(); works just fine.

Hint: This issue is not a bug of the Worklist tutorial or of any other tutorial. I’m not sure if this is expected behavior or not. It feels confusing somehow and I did not find anything documenting this.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:4
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
akudevcommented, Oct 16, 2018

Not sure… not every view is the root view of a component. And when it is not, any parent control/view can have a model which overrides the component model. This would mean that you do get the component model in onInit, but a different model later, once the view is in the UI tree. Sounds confusing to me (even though it might not happen in most straightforward cases).

1reaction
boghyoncommented, Oct 15, 2018

@akudev @codeworrior Would it be appropriate, also in terms of compatibility, to just return Component’s model in such cases? Something like this in sap.ui.core.mvc.View:

View.prototype.getModel = function(sName) {
  var oModel = Control.prototype.getModel.apply(this, arguments);
  if (oModel) {
    return oModel;
  }
  var oController = this.getController();
  if (!this.getParent() && oController && oController.getOwnerComponent()) {
    return oController.getOwnerComponent().getModel(sName);
  }
}

It does seem to confound quite a lot of people that this.getView().getModel() doesn’t return anything in onInit. Getting the component model there would make developing UI5 apps a bit more intuitive I think.

Read more comments on GitHub >

github_iconTop Results From Across the Web

getOwnerComponent() returns undefined in controller of ...
getOwnerComponent() returns the correctly assigned component instead of undefined , and thus this.getOwnerComponent().getModel() works.
Read more >
How to Access the oData Model inside the Init Function of the ...
Hi All,. Can anyone tell how to access the oData Model(Manifest.json) inside the Intit() of the Controller?
Read more >
sap.ui.core.mvc.Controller - API Reference - Demo Kit
Returns the view associated with this controller or undefined . ... getModel() should be used inside onInit() to get a model assigned to...
Read more >
SAPUI5: How to Get a Model in onInit in a Controller? - INUI.io
this.getView().getModel() in a controller's onInit method returns undefined because this.getView().getParent() returns null in a controller's ...
Read more >
SAPUI5 OData CRUD Operations - amarmn.com
Example on OData service with the four basic functionalities of Create, Read, Update and Delete and its consumption in a SAPUI5 application is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found