Component + default OData model: this.getView().getModel() returns undefined in onInit() of controllers
See original GitHub issueSAPUI 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:
- Open the last step of the Worklist tutorial: https://openui5beta.hana.ondemand.com/test-resources/sap/m/demokit/tutorial/worklist/06/webapp/test/testService.html
- go to the App.controller.js of the Worklist tutorial
- set a breakpoint inside the
onInit()
of the controller, i.e. via Chrome dev tools - reload the app and wait until the breakpoint hits
- in the console type:
this.getOwnerComponent().getModel()
==> returns the model - in the console type:
this.getView().getModel()
==> undefined
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:
- Created 8 years ago
- Reactions:4
- Comments:13 (10 by maintainers)
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).
@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
:It does seem to confound quite a lot of people that
this.getView().getModel()
doesn’t return anything inonInit
. Getting the component model there would make developing UI5 apps a bit more intuitive I think.