刷新页面时,sap.m.MessageBox.confirm显示未定义[F5]

时间:2018-07-02 03:41:22

标签: sapui5

每当我使用F5刷新页面时,都会出现错误:

在调试中,我看到totalRtn具有值,所以转到其他位置-当控制权转到sap.m.MessageBox.confirm时,我收到一条错误消息,提示“未捕获的TypeError:无法读取未定义的属性'confirm'”

if (totalRtn <= 0) {
                    sap.m.MessageToast.show("Cannot return 0 quantity");
                } else {

                    sap.m.MessageBox.confirm("You are returning total of " + totalRtn + " items from this order", {
                        actions: [sap.m.MessageBox.Action.YES, sap.m.MessageBox.Action.NO],
                        styleClass: "messageBoxError",
                        onClose: function(oAction) {
                            if (oAction === sap.m.MessageBox.Action.YES) {
                                ServiceUtils.createEntry(this, oEntry, sEntity, sURL).done(function(data) {
                                    var jsonModel = new JSONModel(data);
                                    var returnNumber = jsonModel.getProperty("/data/SalesDocument");
                                    sap.m.MessageToast.show("Return " + returnNumber + " is created successfully");
                                    vc.getOwnerComponent().getRouter().navTo("stockrooms", {
                                        "companyId": vc.companyId
                                    }, false);

                                }).fail(function(error) {
                                    sap.m.MessageToast.show("Error in return processing");
                                });
                            } else {
                                sap.m.MessageToast.show("Return not confirmed");
                            }
                        }
                    });

3 个答案:

答案 0 :(得分:0)

是的,MessageBox是一个静态类,sap.ui.require(“ sap / m / MessageBox”);必须先明确执行该语句,然后才能使用该类。

答案 1 :(得分:0)

基于Jorg在评论中提到的内容,最新的最佳实践描述了如果在某些情况下仅使用控件,则仅应在需要时加载该库。这将提高初始加载性能。

在您的示例中,您仅需要在sap.m.MessageBox大于0时加载totalRtn库。因此,我建议对您提供的代码进行以下更改:

else {
sap.ui.require(["sap/m/MessageBox"], function(oMessageBox) {
    oMessageBox.confirm("You are returning total of " + totalRtn + " items from this order", {
        actions: [sap.m.MessageBox.Action.YES, sap.m.MessageBox.Action.NO],
        styleClass: "messageBoxError",
        onClose: function(oAction) {
            if (oAction === sap.m.MessageBox.Action.YES) {
                ServiceUtils.createEntry(this, oEntry, sEntity, sURL).done(
                    function(data) {
                        var jsonModel = new JSONModel(data);
                        var returnNumber = jsonModel.getProperty("/data/SalesDocument");

                        sap.m.MessageToast.show("Return " + returnNumber + " is created successfully");

                        vc.getOwnerComponent().getRouter()
                        .navTo("stockrooms", 
                            { "companyId": vc.companyId }, 
                            false
                        );
                    }).fail(function(error) {
                            sap.m.MessageToast.show("Error in return processing");
                    });
                } else {
                   sap.m.MessageToast.show("Return not confirmed");
                }
            }
        });
    }.bind(this));
...

答案 2 :(得分:0)

您需要加载模块“ sap.m.MessageBox”

尝试一下:

jQuery.sap.require("sap.m.MessageBox");

if (totalRtn <= 0) {
                    sap.m.MessageToast.show("Cannot return 0 quantity");
                } else {

                    sap.m.MessageBox.confirm("You are returning total of " + totalRtn + " items from this order", {
                        actions: [sap.m.MessageBox.Action.YES, sap.m.MessageBox.Action.NO],
                        styleClass: "messageBoxError",
                        onClose: function(oAction) {
                            if (oAction === sap.m.MessageBox.Action.YES) {
                                ServiceUtils.createEntry(this, oEntry, sEntity, sURL).done(function(data) {
                                    var jsonModel = new JSONModel(data);
                                    var returnNumber = jsonModel.getProperty("/data/SalesDocument");
                                    sap.m.MessageToast.show("Return " + returnNumber + " is created successfully");
                                    vc.getOwnerComponent().getRouter().navTo("stockrooms", {
                                        "companyId": vc.companyId
                                    }, false);

                                }).fail(function(error) {
                                    sap.m.MessageToast.show("Error in return processing");
                                });
                            } else {
                                sap.m.MessageToast.show("Return not confirmed");
                            }
                        }
                    });

致谢!