简体   繁体   中英

Pass variable to jquery modal dialog open function

I am trying to show a jquery modal dialog box on error of two fetch methods. The modal dialogs are same except for the body in open function. I would like to create only a single function and pass the body text as parameters from the fetch methods. How can I do the same?

const fetchCallOne = () => {
  try {
  } catch(err) {
    alertForDialogFetchCallOne.dialog('open');
  }
}

const fetchCallTwo = () => {
  try {
  } catch(err) {
    alertForDialogFetchCallTwo.dialog('open');
  }
}

const alertForDialogFetchCallTOne = $('<div></div>').dialog({
  title: 'Note',
  modal: true,
  autoOpen: false,
  open: function() {
    $(this).html('Fetch Call One Error');
  },
  buttons: {
    Ok: function() {
      $(this).dialog('close');
    },
  },
});

const alertForDialogFetchCallTwo = $('<div></div>').dialog({
  title: 'Note',
  modal: true,
  autoOpen: false,
  open: function() {
    $(this).html('Fetch Call Two Error');
  },
  buttons: {
    Ok: function() {
      $(this).dialog('close');
    },
  },
});

As seen above the only difference is the body of open . So how can I make only one function and call it with parameters?

For eg:

alertForDialogAnyFetchCall.dialog('open').body('Fetch Call One Error');
alertForDialogAnyFetchCall.dialog('open').body('Fetch Call Two Error');

i hope this is what you looking for

 function message(type){ alert(type); }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <button onClick=message("open")> Click me </button>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM