前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SAP UI5 ODataModel.createEntry 单步调试

SAP UI5 ODataModel.createEntry 单步调试

作者头像
Jerry Wang
发布2022-10-06 08:08:53
2950
发布2022-10-06 08:08:53
举报

方法入口:

代码语言:javascript
复制
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 方法。

修改之后的代码:

代码语言:javascript
复制
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:

代码语言:javascript
复制
--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 的响应:

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-10-05,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
批量计算
批量计算(BatchCompute,Batch)是为有大数据计算业务的企业、科研单位等提供高性价比且易用的计算服务。批量计算 Batch 可以根据用户提供的批处理规模,智能地管理作业和调动其所需的最佳资源。有了 Batch 的帮助,您可以将精力集中在如何分析和处理数据结果上。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档