Search code examples
sapui5

Set model app wide or only on view


I have a controller that creates on the onInit function a model:

return Controller.extend("com.mindustry.SalesStatistics2.controller.Content", {

    onInit: function() {
      this.getOwnerComponent().setModel(oModel, "PayersSuggestion");
      .....

The question is, is the model PayersSuggestion globally defined or only on local?

And what is the difference between

this.getOwnerComponent().setModel(oModel, "PayersSuggestion");

and

this.getView().setModel(oModel, "PayersSuggestion");

When I use the second on the onInit function and for example few lines later I would call

const oPayersModel = this.getView().getModel("PayersSuggestion");  

then oPayersModel would be undefined.


Solution

  • Assigning the model to the Component (using getOwnerCOmponent API) will be inherited by the Views as well. (But only after the current view is instantiated). This is because Component is at a higher level in the hierarchy.

    Assigning to the View will be available at the view and also at the controls inside it.