办法入口:

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...$batch

content-type:multipart/mixed;boundary=batch_23c4-3627-5005

body:

--batch_23c4-3627-5005Content-Type: multipart/mixed; boundary=changeset_6ddc-5df2-c1a1--changeset_6ddc-5df2-c1a1Content-Type: application/httpContent-Transfer-Encoding: binaryPOST Customers HTTP/1.1Content-Type: application/jsonContent-ID: id-1664894186776-9sap-contextid-accept: headerAccept: application/jsonAccept-Language: en-USDataServiceVersion: 2.0MaxDataServiceVersion: 2.0Content-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 的响应: