如何从 OData 元数据创建本地空 JSON 模型
How to create a local empty JSON Model from OData metadata
有没有办法从 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>
不确定,您想要什么,但此脚本会将 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
}
});
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
。
有没有办法从 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>
不确定,您想要什么,但此脚本会将 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
}
});
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
。