Search code examples
alertifyjs

How to specify actions for auxiliary buttons in AlertifyJS?


AlertifyJS has a place to put auxiliary buttons.

I would like for two things to happen when my auxiliary button is clicked

  1. The dialog should not close
  2. Some function should be run

How do I do these two things?

I can get the notification to show up by passing it as the third parameter, but the dialog disappears. Also, this wouldn't work if I had multiple auxiliary buttons and different functions for each.

Below is my javascript, and here is a JSFiddle.



    // Run this function when the auxiliary button is clicked
    // And do not close the dialog
    var helpInfo = function () {
        alertify.notify("help help help");
    };

    var custom = function () {
        if (!alertify.helper) {
            alertify.dialog('helper', function factory() {
                return {
                    setup: function () {
                        return {
                            buttons: [{
                                text: 'Help',
                                scope: 'auxiliary'
                            }],
                            options: {
                                modal: false
                            }
                        };
                    }
                };
            }, false, 'alert');
        }
        alertify.helper('Do you need help?', "hello world", helpInfo);
    };

    custom();


Solution

  • AlertifyJS callbacks will be passed a special closeEvent object. To keep the dialog open, your callback should set the cancel property to true or simply return false.

    var helpInfo = function (closeEvent) {    
       alertify.notify("help help help");    
       closeEvent.cancel = true;
      //or
      //return false;
    };
    

    See Updated Fiddle