How to get binding context of the second model?

Viewed 1037

My Fiori application has a view for which two models are set. The first model is set by default in manifest.json (OData), the second model I set in the controller.

The data of the second model are displayed in sap.m.TextArea. If I try to get its binding context, the output is undefined.

This is how I set second model:

onInit: function() {
  var oLocalModel = {
    Text: "test"
  };
  // JSONModel required from "sap/ui/model/json/JSONModel"
  var oModel = new JSONModel(oLocalModel);
  this.getView().setModel(oModel, "localData");
},

This is how I try to get the binding context of the TextArea:

onSendMail: function(oEvent) {
 var oLocalContext = oEvent.getSource().getBindingContext("localData"); // undefined
 var oLocalContext = this.byId("zgutMailFormText").getBindingContext("localData"); // undefined too 
},
<TextArea id="zgutMailFormText" value="{localData>/Text}">
  <layoutData>
    <l:GridData span="XL2 L3 M3 S8" />
  </layoutData>
</TextArea>
1 Answers

How to get the context depends on your application code which I don't see much in your question. But since the localData model is a client-side model, and since the binding path is already known (absolute path), you could simply create the context with the method createBindingContext.

onSendMail: function(oEvent) {
  const oLocalContext = this.getView().getModel("localData").createBindingContext("/");
  // ...
},

There could be many reasons why a control returns no context:

  • The control has simply no context. Contexts are created by the framework automatically for resolving relative binding paths. Since your TextArea binds data with an absolute path (localData>/Text), no context needs to be created. The path can be resolved immediately.
  • The control you accessed (e.g. this.byId("zgutMailFormText")) is part of a template control. Templates don't contain any contexts. Only the rendered clones do. See this answer for more explanation.
  • Parent controls themselves have no contexts bound, so there is no context to propagate.

From the API reference: sap/ui/model/Context:

The Context is a pointer to an object in the model data. A relative binding needs a context as a reference point in order to resolve its path; without a context, a relative binding is unresolved and does not point to model data. Context instances can, for example, be created in the following ways:

Related