如何使用odata服务从manifest.json动态创建模型

如何使用odata服务从manifest.json动态创建模型,odata,sapui5,Odata,Sapui5,我对UI5非常陌生,我正在开发一个应用程序,它要求我根据浏览器(客户端)的请求创建模型。 如果我预先使用了所有odata服务并根据请求使用它们,那么它将变得不必要地太重。 有什么方法可以动态完成吗?我认为您的问题标题和问题内容可能相互矛盾,因此我将分别提出我的建议 如何使用odata服务从manifest.json动态创建模型 在manifest.json文件中,找到“sap.app”部分/属性,然后添加一个数据源,如下所示: "dataSources": { //used data sourc

我对UI5非常陌生,我正在开发一个应用程序,它要求我根据浏览器(客户端)的请求创建模型。 如果我预先使用了所有odata服务并根据请求使用它们,那么它将变得不必要地太重。
有什么方法可以动态完成吗?

我认为您的问题标题和问题内容可能相互矛盾,因此我将分别提出我的建议

如何使用odata服务从manifest.json动态创建模型

在manifest.json文件中,找到“sap.app”部分/属性,然后添加一个数据源,如下所示:

"dataSources": { //used data sources -> ui5-related information stored in sap.ui5 namespace (unique inside the app)
         "modelalias": { //key is alias which is used below in e.g. sap.ui5 ...
             "uri": "/sap/opu/odata/snce/PO_S_SRV;v=2/" , //mandatory; version is part of uri, e.g. ";v=2", default is 1
             "type": "OData" , //OData (default)|ODataAnnotation|INA|XML|JSON
             "settings": { //data-source-type-specific attributes (key, value pairs)
                 "odataVersion": "2.0" , //possible values: 2.0 (default), 4.0
                 "annotations": [ "equipmentanno" ], //filled e.g. for Smart Template
                 "localUri": "model/metadata.xml" //relative url to local metadata
    "maxAge": 360 //time in seconds
    }
         }
    "models": {
...
            "mymodel": { //empty string "" is the default model
                "preload": true; //indicator that the model will be created immediately after the manifest is loaded by component factory and before the component instance is created
                "dataSource": "modelalias", //reference of dataSource under sap.app - only enhance it with more settings for UI5 if needed
                "settings": {
                }
            }
        },
要使用别名“mymodel”实例化此模型,可以在“sap.ui5”下的manifest.json中添加一个条目,如下所示:

"dataSources": { //used data sources -> ui5-related information stored in sap.ui5 namespace (unique inside the app)
         "modelalias": { //key is alias which is used below in e.g. sap.ui5 ...
             "uri": "/sap/opu/odata/snce/PO_S_SRV;v=2/" , //mandatory; version is part of uri, e.g. ";v=2", default is 1
             "type": "OData" , //OData (default)|ODataAnnotation|INA|XML|JSON
             "settings": { //data-source-type-specific attributes (key, value pairs)
                 "odataVersion": "2.0" , //possible values: 2.0 (default), 4.0
                 "annotations": [ "equipmentanno" ], //filled e.g. for Smart Template
                 "localUri": "model/metadata.xml" //relative url to local metadata
    "maxAge": 360 //time in seconds
    }
         }
    "models": {
...
            "mymodel": { //empty string "" is the default model
                "preload": true; //indicator that the model will be created immediately after the manifest is loaded by component factory and before the component instance is created
                "dataSource": "modelalias", //reference of dataSource under sap.app - only enhance it with more settings for UI5 if needed
                "settings": {
                }
            }
        },
现在,清单文件将基于“datasources”中的ODataURI实例化“mymodel”,然后将模型设置到Component.js上。因此,当应用程序启动时,您可以使用以下方式访问任何控制器中的模型:

this.getOwnerComponent().getModel("mymodel")
如果我事先使用了所有odata服务,并根据 如果提出要求,它将变得不必要的沉重。有吗 这样,这可以动态地完成吗

你的假设是创建一个模型会减慢应用程序的启动速度。这可能并不总是正确的,因为:

  • 模型创建非常快
  • 读取数据需要时间和模型实例化
  • ODataModels是异步工作的,因此调用.read或.write是可以异步管理的操作
特殊情况:如果您希望提前(在启动时)预取所有数据,我建议您确保在网关服务上使用$select、$top和$skip等过滤器来实现不断增长的列表式行为

希望这对你有帮助

  • 有关manifest.json的更多信息:
  • 不断增长的名单:
  • ODataModel示例: