cancel
Showing results for 
Search instead for 
Did you mean: 

How to separate callback function from MessageBox.

02-27-2020 9:21 AM
2200 views 5 comments Go to solution
0 Likes
SAP Managed Tags
Subscribe

Hello,

I am trying to create a function to delete data after user press delete button on MessageBox of confirmation.

I know that it will work following code;

MessageBox.confirm(
 "Delete data?", {
 title: "Delete confirmation",
 onClose: function(oAction) {
   switch(oAction) {
     case "OK":
       /* do delete operation */
       break;
     default:
       break;
 }
);

But, I want to know how to separate the callback function from the code of MessageBox as below, becasue this application needs post-processes after deletion so that the code will be longer, I think it should not be included in the code of calling MessageBox.

~~~~~~~
MessageBox.show(
 "Delete data?", {
 title: "Delete confirmation",
 onClose: ".afterDeleteConfirmation"
);

~~~~~~

afterDeleteConfirmation: function(oAction) {
   switch(oAction) {
     case "OK":
       /* do delete operation */
       break;
     default:
       break;
 }
0 Likes

Accepted Solutions (1)

Accepted Solutions (1)

BhargavaTanguturi
Active Participant

Use Promise for callbacks.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

see below code

fnConfirmationMsg: function(sMessage, sConfirmation) {

  return new Promise((fnResolve, fnReject ) => {
        MessageBox.show( sMessage, {
             title: sConfirmation,
             onClose: fnResolve
             }
        );
  });
 };

deleteRecord: function(oData) {
  this.fnConfirmationMsg("Delete data?", "Delete confirmation").then((sAction)=>{
        switch(oAction) {
         case "OK":
            /* do delete operation */
           break;
        default:
           break;
      })
};
Former Member
0 Likes

Thank you.

That's what I wanted. The progrram needs error handling for the delete operation too so that I have to make chain of Promise.

Your code can be used for it too.

Answers (1)

Answers (1)

former_member540067
Active Participant

You can create a separate function and call it from the callback. Use the reference of this but you need to assign it to another variable outside the messagebox function.

~~~~~~~

var that=this;
MessageBox.show(
 "Delete data?", {
 title: "Delete confirmation",
 onClose: function(oAction){
     that.afterDeleteConfirmation(oAction);
     }
);

~~~~~~

afterDeleteConfirmation: function(oAction) {
   switch(oAction) {
     case "OK":
       /* do delete operation */
       break;
     default:
       break;
 }

Regards

Anmol

Joseph_BERTHE1
Active Contributor

Hello,

You can simplify it like this :

MessageBox.show(
 "Delete data?", {
 title: "Delete confirmation",
 onClose: this.afterDeleteConfirmation.bind(this)
);

Regards,

Joseph

Former Member
0 Likes

Your answer also works and I learnt how to call function by using "that". Thank you.