方法入口:
initOData: function(){ function mySuccessHandler(){ debugger; } function myErrorHandler(){ debugger; } var oModel = new ODataModel("http://localhost:8089/Northwind/Northwind.svc/"); var oMetadata = oModel.getServiceMetadata(); var result = oModel.getProperty("/Customer('ALFKI')/Address"); var oContext = oModel.createEntry("/Customers", { properties : {Name : "Laptop X", Description:"New Laptop", Price:"1000", CurrencyCode : "USD"} debugger;
第一个参数为 entitySet 名称,第二个参数为待创建的 entry payload:
需要在 metadata 加载完毕之后,才能调用 createEntry 方法。
修改之后的代码:
sap.ui.define([ "sap/ui/core/UIComponent", "sap/ui/model/json/JSONModel", "sap/ui/model/resource/ResourceModel", "sap/ui/model/odata/v2/ODataModel" ], function (UIComponent, JSONModel, ResourceModel,ODataModel) { "use strict"; return UIComponent.extend("sap.ui5.walkthrough.Component", { metadata : { "interfaces": ["sap.ui.core.IAsyncContentCreation"], "rootView": { "viewName": "sap.ui5.walkthrough.view.App", "type": "XML", "id": "app" } }, initOData: function(){ function mySuccessHandler(){ debugger; } function myErrorHandler(){ debugger; } function myLoaded(oEvent){ debugger; var oContext = oModel.createEntry("/Customers", { properties : {Name : "Laptop X", Description:"New Laptop", Price:"1000", CurrencyCode : "USD"}}); oModel.submitChanges({success: mySuccessHandler, error: myErrorHandler}); // handle successful creation or reset oContext.created().then( function () { /* successful creation */ }, function () { /* deletion of the created entity before it is persisted */ } ); } var oModel = new ODataModel("http://localhost:8089/Northwind/Northwind.svc/"); var oData = { name:'Jerry' }; oModel.attachMetadataLoaded(oData, myLoaded); var oMetadata = oModel.getServiceMetadata(); var result = oModel.getProperty("/Customer('ALFKI')/Address"); debugger; // bind a form against the transient context for the newly created entity // oForm.setBindingContext(oContext); // submit the changes: creates entity in the back end }, init : function () { UIComponent.prototype.init.apply(this, arguments); this.initOData(); var oData = { recipient : { name : "SAP UI5 初学者教程之九 - 创建第一个 Component" } }; var oModel = new JSONModel(oData); this.setModel(oModel); var i18nModel = new ResourceModel({ bundleName: "sap.ui5.walkthrough.i18n.i18n" }); this.setModel(i18nModel, "i18n"); } }); });
现在 metadata 处于成功加载状态,执行 create 函数:
得到 entityType:
this.mChangeGroups 里有一个默认的 change group:
使用默认的 changes group:
生成一个 dummy 的 uid?
submit change:
执行异步队列:
这个 batch 请求什么时候生成的?
HTTP 202 状态码代表的意思是 请求已被接受,但尚未处理,即 HTTP 202 Accepted 响应状态。
这个状态码表明客户端发起的请求,可能会被服务器端处理,也可能会被拒绝,适用于异步操作场合,允许服务器接受其他过程请求,而不用让客户端一直保持与服务器的连接直至批处理操作全部完成。
使用 Postman 给 Northwind 发送 HTTP post 请求创建 customers 的 script:
url:https://services.odata.org/V2/Northwind/Northwind.svc/$batch
content-type:multipart/mixed;boundary=batch_23c4-3627-5005
body:
--batch_23c4-3627-5005 Content-Type: multipart/mixed; boundary=changeset_6ddc-5df2-c1a1 --changeset_6ddc-5df2-c1a1 Content-Type: application/http Content-Transfer-Encoding: binary POST Customers HTTP/1.1 Content-Type: application/json Content-ID: id-1664894186776-9 sap-contextid-accept: header Accept: application/json Accept-Language: en-US DataServiceVersion: 2.0 MaxDataServiceVersion: 2.0 Content-Length: 130 {"Name":"Laptop X","Description":"New Laptop","Price":"1000","CurrencyCode":"USD","__metadata":{"type":"NorthwindModel.Customer"}} --changeset_6ddc-5df2-c1a1-- --batch_23c4-3627-5005--
发送之后,收到 HTTP 202 Accepted 的响应: