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

时间:2017-03-09 20:22:06

标签: odata sapui5

我是UI5的新手,我正在开发一个应用程序,它要求我根据浏览器(客户端)的请求创建模型。 如果我事先消费所有odata服务&根据要求使用它们,不必要地变得太重。 有什么办法,这可以动态完成吗?

1 个答案:

答案 0 :(得分:1)

我认为你的问题标题和问题内容可能是矛盾的,所以我会单独提出我的建议。

  

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

在manifest.json文件中,找到" sap.app" section / property然后按如下方式添加数据源:

"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
    }
         }

要使用别名" mymodel"来实例化此模型,您可以在" sap.ui5"下的manifest.json中添加一个条目。如下:

    "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"基于你的odata uri in" datasources"然后将模型设置到Component.js上。因此,当您的应用程序启动时,您可以使用以下命令访问任何控制器中的模型:

this.getOwnerComponent().getModel("mymodel")
  

如果我事先消费所有的odata服务&根据它使用它们   提出要求,不必要地变得太沉重。有没有   这可以动态完成吗?

您的假设是创建模型会减慢应用启动速度。这可能并非总是如此:

  • 模型创建非常快
  • 读取数据需要时间和模型实例化
  • ODataModels异步工作是默认的,因此调用.read或.write是可以异步管理的操作

特殊情况:如果您希望提前预先获取所有数据(启动时),我建议您确保使用网关服务上的$ select,$ top和$ skip等过滤器来实现增长列表,例如行为。

希望对你有所帮助。

  • 有关manifest.json的更多信息:Link
  • 成长名单:Link
  • ODataModel示例:Link
相关问题