0

I am declaring messageBox inside Controller. I want to use global variable inside onClose Method.

I have tried to pass it inside onClose method as parameter

var that =this;
sap.m.MessageBox.show(
    "Notification " + odata.EvNotificationNo + " has been saved" + attachment_message, 
    {
        icon: sap.m.MessageBox.Icon.SUCCESS,
        title: "Success",
        actions: [
                  "Go to Notification Processing", 
                  sap.m.MessageBox.Action.OK, 
                  sap.m.MessageBox.Action.CANCEL
                 ],
        onClose: function (sAction) {

                 //Here I have use var that

                }
    }
);

I want to use that variable inside onClose method

2 Answers2

1

No variable is available inside onClose method.

Looks like you're trying to access the closure variable while the debugger is paused in that method. That won't work unfortunately in Chromium based browsers. See Why does Chrome debugger think closed local variable is undefined?

When the code is actually run, the variable will be available and properly evaluated.


Apart from the above issue, you can also pass the context reference to the event handler with Function.prototype.bind instead of trying to access that from the method.

// var that = this; <-- instead of doing that
MessageBox.show("...", { // MessageBox required from "sap/m/MessageBox"
  // ...,
  onClose: function(sAction) {
    // this.something instead of that.something
  }.bind(this), // pass `this`
  // ...
});

In contrast to the issue with the closure variable above, the context (this) is always available in the method when the debugger is paused there, from which you can access any properties assigned to this.

Boghyon Hoffmann
  • 17,103
  • 12
  • 72
  • 170
  • @ Boghyon Hoffmann Thanks for the reply I have tried already above solution but it is not working. – Niranjan Nimbarte May 06 '19 at 09:57
  • @NiranjanNimbarte Could you specify what exactly is not working? Are there any errors in the console? The above code is plain JS which should work in any decent browsers. – Boghyon Hoffmann May 07 '19 at 09:32
0

Declare your variable outside show function

var that = this;
var myGlobalVar; //Declare global variable

sap.m.MessageBox.show(
  "Notification " + odata.EvNotificationNo + " has been saved" + attachment_message, {
    icon: sap.m.MessageBox.Icon.SUCCESS,
    title: "Success",
    actions: ["Go to Notification Processing", sap.m.MessageBox.Action.OK, sap.m.MessageBox.Action.CANCEL],
    onClose: function(sAction) {


      myGlobalVar = "foo"; //Set global variable


      if (sAction == "Go to Notification Processing") {

        if (sap.ushell && sap.ushell.Container && sap.ushell.Container.getService) {
          var oCrossAppNavigator = sap.ushell.Container.getService("CrossApplicationNavigation");

          oCrossAppNavigator.toExternal({
            target: {
              semanticObject: "ZUI5_8FNPR_SEMR",
              action: "execute"
            }, //the app you're navigating to 
            params: {
              EvNotificationNo: odata.EvNotificationNo
            }
          });
        } else {
          jQuery.sap.log.info("Cannot Navigate - Application Running Standalone");
        }

      }
    }.bind(that)
  }
);
alexP
  • 3,672
  • 7
  • 27
  • 36