0

I am trying to define a global model in one of my controllers and then trying to get its value in some XML Views but I don't know why it doesn't show up.

Controller :

sap.ui.getCore().setModel(oModelo, "miModelo");

View :

<Text id="selectiona"
  class="selectiona"
  text="{= ${miModelo>/miSelectiona}.length > 0 ? 'Selected:' + ${miModelo>/miSelectiona} : ''}"
/>

P.S: it works when I s set the model like this:

this.byId("selectiona").setModel(oModelo,"miModelo");

But I need to define it globally. Is there any way I can do that?

Mebin Joe
  • 2,172
  • 4
  • 16
  • 22
Gregor Samsa
  • 115
  • 12

2 Answers2

1

As mentioned in this answer, models set on the Core are not propagated to the children of the ComponentContainer. Therefore, set the model on the Component instead. You can do this either declaratively in manifest.json or by calling this.setModel(...) in Component.js. Then, the model will be propagated automatically to its children, meaning the view will be able to display the model data without any involvement of any controller. If required, you can still access the model from the any controller by calling this.getOwnerComponent().getModel(...).

Examples:

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
0

Declare it on manifest.json. Here is an example:

"models": {
        "i18n": {
            "type": "sap.ui.model.resource.ResourceModel",
            "settings": {
                "bundleName": "atendimentoMobile.i18n.i18n"
            }
        },
        "viewModel": {
            "type": "sap.ui.model.json.JSONModel",
            "dataSource": "viewModelDs",
            "settings": {
                "defaultBindingMode": "TwoWay"
            }
        },
        "deviceModel": {
            "type": "sap.ui.model.json.JSONModel",
            "dataSource": "deviceDs",
            "settings": {
                "defaultBindingMode": "OneWay"
            }
        },
        "authModel": {
            "dataSource": "authServiceDs",
            "settings": {
                "defaultBindingMode": "TwoWay",
                "useBatch": false
            }
        },
        "chamadosModelOdata": {
            "dataSource": "mainServiceDs",
            "settings": {
                "defaultBindingMode": "TwoWay",
                "useBatch": false
            }
        }
    }
Geraldo Megale
  • 363
  • 1
  • 10