2017-03-09 90 views
1

我对UI5非常陌生,我正在研究一个应用程序,该应用程序需要我根据浏览器(客户端)提出的请求创建模型。 如果我事先使用了所有的odata服务&根据所做的请求使用它们,那么它会变得太重而不必要。 有什么办法,这可以动态地完成吗?如何使用odata服务从manifest.json动态创建模型

回答

1

我认为你的问题标题和问题内容可能是相互矛盾的,所以我将我的建议分开。

如何使用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 
    } 
     } 

实例化这个模型以别名“为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": { 
       } 
      } 
     }, 

现在清单文件将根据您的odata uri在“datasources”中实例化“mymodel”,然后将模型设置到您的Component.js中。您的应用程序启动,这样的情况下,你可以使用访问模型中的任何控制器:

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

如果我消耗所有事先&的OData的服务,根据 的要求而作出使用它们,就会显得过于沉重不必要的。有没有任何 的方式,这可以做到动态?

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

  • 该模型的创建是非常快的
  • 读取数据就是需要时间和模型实例
  • ODataModels做工asyncronously被默认所以调用.read或.WRITE的操作是可以异步管理

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

希望能帮到你。 Link

  • 越来越多:Link
  • ODataModel例子:Link
  • 上的manifest.json

    • 更多信息