简体   繁体   English

SAPUI5 使用值来自 JSON Model 1 在 JSON ZA559B87068921EEC08ZE59

[英]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?我可以使用/绑定第一个 JSON Model 中的值吗? In the example below the text of the first item in the second model (MyNamedModel) should be 'Apple'.在下面的示例中,第二个 model (MyNamedModel) 中第一个项目的文本应为“Apple”。

Complete example can be found here: https://plnkr.co/edit/WtStoqxsjUAfC9Ia完整的例子可以在这里找到: 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.用例:实际上我有一个 geoMap 控件,其中 mapConfiguration 在 JSONModel 中,我想从后端获取一个特定的参数值 (URL) 并在 mapConfiguration 中使用它。

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".基本上使用this.getView().getModel("FruitModel").getData()语句,您可以从“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这也是您的工作示例https://plnkr.co/edit/7EO3qaXPpHX5QUZM

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM