天天看點

SAP OData 的 V2 模型

文檔位址: https://sapui5.hana.ondemand.com/#/topic/6c47b2b39db9404582994070ec3d57a2#loio66a130fa4d10411b8fc90df00185554b

The OData model is a server-side model, meaning that the data set is only available on the server and the client only knows the currently visible (requested) data. Operations, such as sorting and filtering, are done on the server. The client sends a request to the server and shows the returned data.

OData 模型是一個伺服器端模型,這意味着業務資料隻在伺服器端可用,用戶端隻維護了其目前送出請求後,伺服器端傳回的部分資料。圍繞資料進行的排序和過濾等操作,都是先在伺服器端執行,再将執行結果傳回給用戶端。

Requests to the back end are triggered by list bindings (ODataListBinding), context bindings (ODataContextBinding), and CRUD functions provided by the ODataModel. Property bindings (ODataPropertyBindings) do not trigger requests.

目前 OData 模型通過如下 API 觸發向背景的資料請求:

List binding - ODataListBinding

context binding - ODataContextBinding

增删改查操作

The following two versions of the OData model are implemented:

sap.ui.model.odata.ODataModel

sap.ui.model.odata.v2.ODataModel.

The v2.ODataModel has an improved feature set and new features will only be implemented in this model.

sap.ui.model.odata.ODataModel is deprecated. We recommend to only use v2.ODataModel.

前者已經被标注成 deprecated 狀态,後者是 SAP 推薦使用的資料模型。

注意:OData V2 模型已經支援用戶端的排序和過濾了。

SAP OData 的 V2 模型
OData model 模型執行個體的建立方法:

var oModel = new ODataModel("http://services.odata.org/Northwind/Northwind.svc/");
var oModel = new ODataModel({serviceUrl: "http://services.odata.org/Northwind/Northwind.svc"});      

當模型執行個體建立時,自動觸發擷取 OData 服務中繼資料的請求:

http://services.odata.org/Northwind/Northwind.svc/$metadata

Only the first model instance triggers a $metadata request. A JSON representation of the service metadata can be accessed by calling the getServiceMetadata() method on an OData model instance.

JSON 格式的服務中繼資料,通過模型執行個體的 getServiceMetadata 方法傳回。

var oMetadata = oModel.getServiceMetadata();      

In the v2.ODataModel, the service metadata is loaded asynchronously. It is not possible to load it synchronously. To get notified when the loading is finished, attach the metadataLoaded event.

對于 2.0 版本的 OData 模型來說,服務中繼資料采取一部方式加載,如果在應用程式裡想捕捉中繼資料加載成功的事件,需要監聽 metadataLoaded 事件。