2

The common way to make a data binding in UI5 is via this.getView().getModel():

const wantedModel = this.getView().getModel("wantedModel");

this.getView().setModel(wantedModel);

However, it's possible to perform a binding via this.getOwnerComponent().getModel() as well:

const wantedModel = this.getOwnerComponent().getModel("wantedModel");

this.getView().setModel(wantedModel);

In my case, the data model is defined in manifest.json, and I have two options to retrieve a model for a binding:

  • via getView() in onBeforeRendering(), which is called every time a view is rendered
  • via getOwnerComponent() in onInit(), which is called only once.

To avoid setting a model every time a view is rendered, I consider using the second approach. What are the possible advantages/drawbacks, primarily performance-related, of getting a model via getOwnerComponent() over getView()?

Mike
  • 14,010
  • 29
  • 101
  • 161
  • 1
    For other readers regarding `onBeforeRendering`: https://stackoverflow.com/q/55082731/5846045 – Boghyon Hoffmann Oct 05 '22 at 15:21
  • 1
    Not performance related, but does https://stackoverflow.com/a/42251431/5846045 help? – Boghyon Hoffmann Oct 05 '22 at 15:25
  • @BoghyonHoffmann, yes. Now, I have an expert approval of my assumption and can use `getOwnerComponent()` for a model retrieval with more confidence. Pls, feel free to arrange a response as an answer, so I can accept it. – Mike Oct 05 '22 at 15:56
  • 1
    Keep in mind that `getOwnerComponent()` could also return `undefined` if the corresponding view was created and embedded manually via e.g. `XMLView.create`: https://stackoverflow.com/q/63595938/5846045 – Boghyon Hoffmann Oct 05 '22 at 16:18
  • TBH I didn't actually fully understand your question. Were you asking where to set the model? The code snippets are a bit confusing to me. – Boghyon Hoffmann Oct 05 '22 at 16:20
  • The initial question was: what would be better to retrieve the model via `getView()` in `onBeforeRendering()` or via `getOwnerComponent()` in `onInit()`. But since such kind of questions get closed as opinion based, I've elaborated it. – Mike Oct 05 '22 at 16:29

1 Answers1

1

In the base controller, from which all other controllers will be inherited, create a method like this:

sap.ui.define([
    "sap/ui/core/Core"
], (Core) => Controller.extend("com.myapp.controller.BaseController", {

    getModel(sName) {
        const dataModel = this.getView().getModel(sName)
                            || this.getOwnerComponent().getModel(sName)
                            || Core.getModel(sName);

        return dataModel; 
    }
}));

Then put the base controller in your custom library and reuse it in the future.

Mike
  • 14,010
  • 29
  • 101
  • 161
Alex
  • 101
  • 3
  • Thanks, that's possible approach, although I would recommend to avoid calling global methods, e.g. `sap.ui.getCore()`. This might prevent module's async loading. Instead, it's better to load module via `sap.ui.define` and then to call its methods. – Mike Oct 06 '22 at 19:04