通过SAPUI5中的批处理操作更新oData [英] Updating oData via Batch Operation in SAPUI5

查看:615
本文介绍了通过SAPUI5中的批处理操作更新oData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面列出了以下代码:

I have the following code listed below:

oTable = sap.ui.getCore().byId('tableID');
var index = oTable.getSelectedIndex();

if (index > -1) {
    var currentRowContext = oTable.getContextByIndex(index);
    var model = oTable.getModel();
    var name = model.getProperty("NAME", currentRowContext);

    var phase = model.getProperty("PHASE", currentRowContext);
    var originalPhase = Phase;
    var sequence;

    var batchChanges = [];

    jQuery.ajax({
        type: "GET",
        contentType: "application/json",
        url: "http://urlpathforxsodata/customer.xsodata/TOOL/?$filter=NAME eq '" + name + "'",
        dataType: "json",
        async: false,
        success: function(data, textStatus, jqXHR) {

            // After processing while loop, index will be the
            // first non-frozen record
            while (originalPhase == phase) {
                index = index + 1;
                phase = data.d.results[index].PHASE;
            }

            for (var i = 0; i < index; i++) {
                var frozenFlag = data.d.results[i].FROZEN_FLG;
                if (frozenFlag != 1) {
                    sequence = data.d.results[i].SEQUENCE;
                    var entry = {
                        NAME: name,
                        SEQUENCE: sequence,
                        FROZEN_FLG: 1,
                    };

                    batchChanges.push(model.createBatchOperation(
                        "/TOOL(NAME='" + name + "',SEQUENCE=" + sequence + ")",
                        "PUT",
                        entry)
                    );
                }
            }

            model.addBatchChangeOperations(batchChanges);

            // submit changes and refresh the
            // table and display message
            model.submitBatch(function(data) {
                model.refresh();
            });
        }
    });
}

代码的开头部分是业务逻辑,我已对其进行验证,以确定我想更新哪些行.我使用Ajax返回与表中显示的数据集完全相同的数据集(无法对表进行过滤或排序,因此索引将匹配).我的问题与我正在进行的录入和批处理调用有关.它无法正常工作,并且我收到了以下命令:

The beginning part of the code is business logic that I've verified to determine which rows I would like to update. I use Ajax to return a data set exactly as what is shown in the table (table cannot be filtered or sorted upon so indices will match). My question is related to the entry and batch calls I am making. It's not working and I'm getting the following order:

POST/FiringOrder.xsodata/TOOL/Services/customer.xsodata/$batch

POST /FiringOrder.xsodata/TOOL /Services/customer.xsodata/$batch

"2014-11-06 11:26:02发生以下问题:HTTP请求失败400,错误请求,{"错误:{"代码:","消息:{" lang:" en -US," value:"资源路径中位置29的语法错误.}}}-" sap-ui-core.js:80

"2014-11-06 11:26:02 The following problem occurred: HTTP request failed400,Bad Request,{ "error": { "code": "", "message": { "lang": "en-US", "value": "Syntax error in resource path at position 29."}}} - " sap-ui-core.js:80

"2014-11-06 11:26:02发生以下问题:HTTP请求失败400,错误请求,{"错误:{"代码:","消息:{" lang:" en -US," value:"资源路径中位置29的语法错误.}}}-"

"2014-11-06 11:26:02 The following problem occurred: HTTP request failed400,Bad Request,{ "error": { "code": "", "message": { "lang": "en-US", "value": "Syntax error in resource path at position 29."}}} - "

三个问题:

  1. 是否有更好的方法来调试http请求错误? (对不起,我真的很陌生!)
  2. 我的批次或条目格式错误吗?我对我通过参数传递的URL路径(名称和序列都是记录的键)以及是否应该使用PUT进行更新感到怀疑.尝试MERGE不会做任何事情,除了会立即给我一个差异错误,而且错误消息不可用.
  3. 最后但并非最不重要的是,所有这些逻辑都在我的控制器中.是对的还是应该在服务器上完成这种类型的逻辑?

推荐答案

首先,它真的很难阅读您的代码:-)检查此文档:

first of all, its really hard to read your code :-) check this documentation:
SAPUI5 - Batch Operations - how to do it right?

关于1)您收到的错误消息是来自后端(SAP)的响应,它与您在事务/IWFND/ERROR_LOG中得到的错误相同.在我看来,此信息就足够了,BAD REQUEST表示您请求(POST)格式错误,您的逻辑会在后端引发转储-有关详细信息,请检查后端.我认为通过AJAX记录成功/错误消息的方式是调试HTTP请求的最常见方式.

about 1) what you get as error message is the response from the backend (SAP), its the same as the error you get in transaction /IWFND/ERROR_LOG .. in my opinion this information is enough, BAD REQUEST says your request (POST) is malformed, your logic raises a dump in the backend - for details check the backend. I think your way by logging success/error message from AJAX is the most common for debugging http requests.

大约2)我认为是的,首先它表示请求错误(请检查后端以获取错误日志).否则,您是否在控制器中初始化了模型? Odata POSTing仅在Odata Model上有效,当前您正在尝试为表模型调用它吗?我认为那不起作用,因此我建议保持逻辑直到"model.addBatchChangeOperations(batchChanges);"为止.提交,然后:

about 2) I think yes, first of all it says bad request (check backend for error log). Otherwise did you initialize your model in the controller? Odata POSTing works only on Odata Model, currently you are trying to call it for the table model? I think thats not working, so I would propose keeping the logic till "model.addBatchChangeOperations(batchChanges);" submit and then:

onInit : function() { // in controller
    // register the central service (for create/update/other operations)
    var sServiceUrl = "/sap/opu/odata/sap/YOUR_SRV/";
    var oModel = new sap.ui.model.odata.ODataModel(sServiceUrl, true);
    sap.ui.getCore().setModel(oModel);
}

然后查看或在任何地方提交:

then in view or whereever you submit:

var oModel = sap.ui.getCore().getModel(); // ensure that you did not overwrite your 
                                          // model locally!! with some JSON model for example
oModel.addBatchChangeOperations(batchChanges);
oModel.setUseBatch(true);
oModel.submitBatch();

大约3)如果您在本地(在前端)处理要发布的数据,我认为控制器很好(恕我直言).

about 3) If you are working locally (in the frontend) with data which you want to post, I think the controller is fine (imho).

进一步的评论:据我所知,您想在表中重新加载数据,我认为通过发布数据,获取结果然后将数据重新绑定到表中可以更轻松地完成操作.

Further Comment: As far as I get it, you want to reload data in a table, I think this could be done easier by posting the data, fetching the result, then rebind the data to the table.

关于zy

这篇关于通过SAPUI5中的批处理操作更新oData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆