在“返回”之前调用“ oDataModel.read”

时间:2019-07-18 11:24:12

标签: javascript promise odata sapui5 sap-fiori

在我的returns语句之前调用执行函数oDataModel.read的语句时,这会导致传递空参数。

如何先调用oDataModel.read,然后再调用return语句。

var sUrl = "/sap/opu/odata/sap/(some name)/";
        var oDataModel = new sap.ui.model.odata.v2.ODataModel(sUrl, false);
        var oPlant = new sap.ui.model.Filter("Plant", sap.ui.model.FilterOperator.EQ, oContext.getAllData().Plant);
        var oStatus = new sap.ui.model.Filter("Status", sap.ui.model.FilterOperator.EQ, oContext.getAllData().Status);
        var oPriority = new sap.ui.model.Filter("priority", sap.ui.model.FilterOperator.EQ, oContext.getAllData().priority);
        var oFilter = new Array(new sap.ui.model.Filter({
            filters: [oPlant, oStatus, oPriority],
            and: true
        }));

            oDataModel.read("(some name)", {
            filters: oFilter,
            success: function (results, error) {
                //sap.m.MesageToast.show(results);
                var data = results.results;

                for (var i = 0; i < data.length; i++) {
                    aCustomSelectionVariant07.push({
                        path: "InspectionLot",
                        value1: data[i].InspectionLot,
                        operator: "EQ",
                        value2: null,
                        sign: "I"
                    });
                }
                //      var aCustomSelectionVariant07 =[];
                var oCustomSelectionVariant07 = {
                    path: "InspLotRsltRecgStatus",
                    operator: "BT",
                    value1: "1",
                    value2: "2",
                    sign: "I"
                };
                aCustomSelectionVariant07.push(oCustomSelectionVariant07);

                oCustomSelectionVariant07 = {
                    path: "InspectionLotType",
                    operator: "BT",
                    value1: "10",
                    value2: "11",
                    sign: "I"
                };
                aCustomSelectionVariant07.push(oCustomSelectionVariant07);


            },
            error: function (error) {
                sap.m.MesageToast.show(error);

            }
            return {
                selectionVariant: aCustomSelectionVariant07,
                ignoreEmptyString: true
            };
},

以上代码return中的

首先被调用,然后用于DataModel.read

我要先DataModel.read,然后打电话返回。

3 个答案:

答案 0 :(得分:0)

您可以将退货放入成功呼叫中:

oDataModel.read("(some name)", {
        filters: oFilter,
        success: function (results, error) {
            //sap.m.MesageToast.show(results);
            var data = results.results;

            for (var i = 0; i < data.length; i++) {
                aCustomSelectionVariant07.push({
                    path: "InspectionLot",
                    value1: data[i].InspectionLot,
                    operator: "EQ",
                    value2: null,
                    sign: "I"
                });
            }
            // var aCustomSelectionVariant07 =[];
            var oCustomSelectionVariant07 = {
                path: "InspLotRsltRecgStatus",
                operator: "BT",
                value1: "1",
                value2: "2",
                sign: "I"
            };
            aCustomSelectionVariant07.push(oCustomSelectionVariant07);

            oCustomSelectionVariant07 = {
                path: "InspectionLotType",
                operator: "BT",
                value1: "10",
                value2: "11",
                sign: "I"
            };
            aCustomSelectionVariant07.push(oCustomSelectionVariant07);

            return {
                selectionVariant: aCustomSelectionVariant07,
                ignoreEmptyString: true
            };
        },
        error: function (error) {
            sap.m.MesageToast.show(error);
        }
}

答案 1 :(得分:0)

欢迎使用异步编程。您有两种选择:

  • 将回调传递给处理返回值的函数 (不爽)
  • 使用诺言(很酷)

我将尝试引导您完成第二个(很酷)的选择。

假设您有一个可以异步处理的功能

getSomeStuff: function() {
    // some code
    oModel.read(sPath, {
        success: function() {
            myStuff = [1,2,3,4];
        },
        error: function() {}
    });

    // when this line is reached, success has not been called and myObject is empty
    return myStuff;
}

您这样称呼

myOtherMethod: function() {
    // will be empty
    var stuff = this.getSomeStuff();
}

您可以将其转换为承诺:

getSomeStuff: function() {
    return new Promise (function(resolve, reject) {
        // some code
        oModel.read(sPath, {
            success: function() {
                myStuff = [1,2,3,4];
                // tell the caller that the promise is fullfilled
                // also pass the data to the caller
                resolve(myStuff);
            },
            error: function() {}
        });
    });
}

并这样称呼它

myOtherMethod: function() {
    this.getSomeStuff().then(function(stuff) {
        // stuff contains [1,2,3,4]
    });
}

答案 2 :(得分:-1)

谢谢您的回答, 通过更改OdataModel版本进行了修复, 之前使用的是(sap.ui.model.odata.v2.ODataModel),所以Async:false无法正常工作, 通过将其更改为(sap.ui.model.odata.ODataModel),它可以正常工作。 请看一下代码。

var oDataModel = new sap.ui.model.odata.ODataModel(sUrl);

	oDataModel.read("/someName", {
				async: false,
				filters: oFilter,
				success: function (results, error) {
					var data = results.results;
          }