【问题标题】:How to create a local empty JSON Model from OData metadata如何从 OData 元数据创建本地空 JSON 模型
【发布时间】:2019-09-03 17:50:20
【问题描述】:

有没有办法从 oData 服务的元数据创建一个本地空 JSON 文件(填充所有实体和属性)?我需要这个用于创建场景,我可以将属性绑定到视图控件。我尝试了以下代码,但没有奏效。将感谢您的建议。

this.getOwnerComponent().getModel().getMetaModel().getODataEntitySet("EntitySetName");

错误:

Uncaught TypeError: Cannot read property 'getObject' of null

at constructor.h.getODataEntityContainer (ODataMetaModel-dbg.js:692)
  at constructor.h.getODataEntitySet (ODataMetaModel-dbg.js:731)
  at eval (eval at _prepareCreatePage (ObjectPage.controller.js:74), <anonymous>:1:48)
  at f._prepareCreatePage (ObjectPage.controller.js:74)
  at f._onObjectPatternMatched (ObjectPage.controller.js:40)
  at constructor.b.fireEvent (EventProvider-dbg.js:228)
  at constructor.<anonymous>

【问题讨论】:

  • 我建议你使用createEntry并绑定它。
  • 您能否详细说明您希望通过阅读元数据来实现什么?您想显示一些注释(例如sap:label="...")吗?我们处理的是什么版本的 OData? V2 还是 V4?

标签: sapui5


【解决方案1】:

SAP 在他们的示例页面中做了非常相似的事情:https://sapui5.hana.ondemand.com/#/entity/sap.ui.table.Table/sample/sap.ui.table.sample.OData2/code

您的第一部分或多或少是正确的,这是一个将元模型存储在单独 JSONModel 中的函数:

function () {
    const that = this;

    const oModel = this.getOwnerComponent().getModel();
    const oMetaModel = oModel.getMetaModel();

    oMetaModel.loaded().then(function () {
        that.setModel(oMetaModel, "meta");
    });
}

有趣的部分是如何访问东西:

const sBasePath = "/dataServices/schema/[${namespace}===\'NAME_OF_YOUR_ODATA_SRV\']/entityType/[${name}===\'NameOfYourEntity\']"
const oEntityInformation = oMetaModel.getProperty(sBasePath);

const aKeys = oMetaModel.getProperty(sBasePath + "/key/propertyRef");
const aAllProperties = oMetaModel.getProperty(sBasePath + "/property");
const oSingleProperty = oMetaModel.getProperty(sBasePath + "/property/[${name}===\'NameOfYourProperty\']");

您甚至可以在 XML 视图中访问模型:

columns="{
    path: 'meta>/dataServices/schema/[${namespace}===\'NAME_OF_YOUR_ODATA_SRV\']/entityType/[${name}===\'NameOfYourEntity\']/property',
    factory: '.columnFactory'
}" 

注意NameOfYourEntity 必须是单个实体的名称,所以末尾没有Set

【讨论】:

    【解决方案2】:

    不确定,您到底想要什么,但此脚本将以 JSON 格式将元数据写入 blob 中。之后会出现浏览器中的保存对话框。

    var oDataModel = this.getOwnerComponent().getModel();
    
    oDataModel.attachMetadataLoaded(null, function() {
      var oMetaData = oDataModel.getServiceMetadata(); //Read the metadata
      var blob = new Blob([JSON.stringify(oMetaData)], {
        type: "text/plain;charset=utf-8"
      }); //Create a blob with metadata string in JSON format
    
      if (navigator.msSaveBlob) {
        return navigator.msSaveBlob(blob, "metadata.json"); //For IE
      } else {
        //For Chrome and FF we create a Link with a download attribute and trigger the click on it
        var sUrl = URL.createObjectURL(blob); //Create the URL for the blob object
        var oLink = document.createElement("a"); //Create link element
        oLink.download = "metadata.json" //Set download attribute for link
        oLink.href = sUrl; //Set href attribute for link
        document.body.appendChild(oLink); //Append link to body
        oLink.click(); //Click on link
        oLink.remove(); //Remove link
      }
    });
    

    【讨论】:

      猜你喜欢
      • 2023-03-08
      • 2021-07-29
      • 2015-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      • 1970-01-01
      相关资源
      最近更新 更多