在UI5下初始化一个OData Model


Instantiating an OData Model
使用OData model 将控件和源自OData services的数据之间进行绑定。

Context

OData model属于server-side model。数据只存在于server端,client端只保有页面上已知的行和字段。Sorting和filtering 是在server端完成的. Client端必须发送request到server端来完成这些任务。OData model目前支持OData version 2.0.

Note
注意浏览器的Same-Origin-Policy会阻止访问不同域名的地址。

Procedure

1. 初始化一个OData Model:

var oModel = new sap.ui.model.odata.ODataModel("http://services.odata.org/Northwind/Northwind.svc/");

根据控件中定义的data binding,系统会自动向server端获取数据。
构造函数的第一个参数就是service URL。如果你需要访问多个service,就必须创建多个对应的OData model的实例。

2. 为OData model增加额外的参数。
在使用OData services时, 可以使用URL parameters实现不同的配置。
SAPUI5 会根据data binding自动设置大部分需要的URL parameters。
增加自定义的参数有两种形式:

  • 添加URL parameters 跟在service URL之后 (option1)
  • 或者使用bindElement或bindAggregation时候,可以传入a map of parameters (option2)

如果使用在OData service URL后面增加URL parameters的形式,这些参数将会包含在每一次发送到OData server的请求之中。
这种形式适用于类似authentication tokens或者是一些通用配置选项。

var oModel = new sap.ui.model.odata.ODataModel("http://myserver/MyService.svc/?custom1=value1&custom2=value2");

3. 无需每次请求都发送的参数
对于其它一些参数,没有必要每次request都发送。这些参数应该被加入特定的 aggregation 或是element binding。例如:expand 或select
对于这种情况,binding方法可以传入一组参数的映射表, 而后仅在这个特定的binding当中会包含这些参数。目前,ODataModel只支持了expand 和select。

  • 例,使用$expand参数:
oControl.bindElement("/Category(1)", {expand: "Products"}); 

oTable.bindRows({ 
  path: "/Products", 
  parameters: {expand: "Category"}
});

在第一个例子中,一次请求之后,所有属于Category(1)的products会包含在server的response里。
在第二个例子中,每一个product对应的category会被包含在每个product对应的response中输出。

  • 例,使用$select
    该参数使请求的对象只返回指定的properties.
oControl.bindElement("/Category(1)", {expand: "Products", select: "Name,ID,Products"}); 

oTable.bindRows({ 
  path: "/Products", 
  parameters: {select: "Name,Category"}
});

在第一个例子中,返回的是Category(1)的"Name"和“ID”,以及products中所有的properties。
In the first example the properties Name and ID ofCategory(1)
在第二个例子中,返回的是每个product的“Name”和“Category”属性。Category属性会同时包含一个指向对应category entry的链接。

4. 添加自定义的query option
可以在URL后面自定义query option。注意query option不是以$开头:

http://services.odata.org/OData/OData.svc/Products?x=y

当然也可以在aggregation binding时传入参数:

oTable.bindRows({
  path: "/Products", 
  parameters: { 
    custom: { param1: "value1", param2: "value2" }
   },
  template: rowTemplate 
});

或者是bindElement:

oTextField.bindElement("/GetProducts", { custom: { "price" : "500" } });

5. Create virtual entries.
对于某个collection,可以创建空的对象记录进行绑定。你可以调用submitChanges来决定,何时将这些对象保存到后台server端。Application必须显式地调用这个方法。
在数据被持久化到后台之前,所有创建的虚拟记录会被保存在一个request queque当中,仍有机会被删除。Application可以决定创建的对象中包含哪些property,也可以为这些字段设置默认值。否则,字段的值就为空。
必须确保Dataset和property在OData Service上存在并且定义在metadata document里面。

例子:

// create an entry of the Products collection with the specified properties and values 
var oContext = oModel.createEntry("Products",{ID:99,Name:"Product",Description:"new Product",ReleaseDate:new Date(),Price:"10.1",Rating:1}); 
// use the returned context for binding against this entry
oLayout.setBindingContext(oContext); 
oSimpleListBox.addItem(new sap.ui.core.ListItem({text: "Added entry: " + oContext.getPath()})); 
// to delete a created entry from the request queue without persisting it provide the created context to the deleteCreatedEntry function 
oModel.deleteCreatedEntry(oContext);

createEntry方法返回一个context对象,可以用它来绑定新建的entry。

下面的例子是如何将request queque当中的entry写回到后端:

// provide optional success and error functions which are called for each request in the queue oModel.submitChanges(fnSuccess, fnError);

Next Steps

OData model的默认绑定模式是单向(one-way)的。双向(two-way)绑定也是可以实现的。参考 OData Write Support.

API Reference:

In this section:
Binding Path Syntax for OData Models
针对OData model的binding path语法,用于获取特定的entry或者collectio。
OData Write Support
SAPUI5 所支持的一些feature.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容