Cruncher
Cruncher

Reputation: 187

SAPUI5 use value from JSON Model 1 in JSON Model 2

Can I use/bind a value from a first JSON Model in a second JSON Model? In the example below the text of the first item in the second model (MyNamedModel) should be 'Apple'.

Complete example can be found here: https://plnkr.co/edit/WtStoqxsjUAfC9Ia

var oFruits = {
    first: "Apple",
    second: "Banana"
  }
  var oFruitModel = new JSONModel(oFruits);
  this.getView().setModel(oFruitModel, "FruitModel");

    var oData = {
      "items": [
        {   Key: "A",
            Text: "{path: '/FruitModel>/first'}",
            Icon: "sap-icon://nutrition-activity"
        },
        {   Key: "B",
            Text: "Paper Plane",
            Icon: "sap-icon://paper-plane"
        },
        {   Key: "C",
            Text: "Vacation",
            Icon: "sap-icon://general-leave-request"
        }
      ]
    };

    var oMyModel = new JSONModel(oData);
    this.getView().setModel(oMyModel, "MyNamedModel");

Use case: In reality I have a geoMap control where the mapConfiguration is in a JSONModel and I would like to get one specific parameter value (URL) from the backend and use it in the mapConfiguration.

Upvotes: 0

Views: 96

Answers (1)

Marco_Polo
Marco_Polo

Reputation: 56

Yes, you can bind it. For the following example, it will look like this:

var oData = {
          "items": [
            {   Key: "A",
          //  Text: "Apple",
            Text: this.getView().getModel("FruitModel").getData().first,
            Icon: "sap-icon://nutrition-activity"
            }
          ]
        };

Basically with the statement this.getView().getModel("FruitModel").getData() you receive access to the whole structure from the "FruitModel". Furthere one, you may assign it's values whatever you'd like to.

Here is also your working example https://plnkr.co/edit/7EO3qaXPpHX5QUZM

Upvotes: 0

Related Questions