3

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();

mikeazo
  • 389
  • 2
  • 24

2 Answers2

1

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

MK.
  • 5,139
  • 1
  • 22
  • 36
  • I actually just figured that out and was going to type out a response. I updated the fiddle myself and noticed that it put /2/ in the URL, so I tried /1/ and saw yours. So I figured I'd wait until you posted an answer. :) – mikeazo Mar 06 '15 at 18:28
  • If I have multiple auxiliary buttons, can they each have a different callback or do I have to have a single callback that figures out which button was clicked? – mikeazo Mar 06 '15 at 18:29
  • Since your dialog is based on `alert`, you have one callback and it will be called for all of your buttons. see https://github.com/MohammadYounes/AlertifyJS/blob/master/src/js/alert.js#L84-L91 – MK. Mar 06 '15 at 18:34
  • The `closeEvent` also provides another two properties `index` and `button` see http://alertifyjs.com/factory.html – MK. Mar 06 '15 at 18:40
  • If you wish to create a new type of dialog with separate callbacks, take a look at the prompt dialog https://github.com/MohammadYounes/AlertifyJS/blob/master/src/js/prompt.js – MK. Mar 06 '15 at 18:44
  • Not works in rthe latest version 2022. helpInfo is never called – JRichardsz Sep 09 '22 at 07:22
0

Add an additional parameter to the helpInfo function so you can access the Event object passed along with the event. Now you can prevent it's default action (which would close the dialog).

var helpInfo = function (e) {
    alertify.notify("help help help");
    e.preventDefault();
};
suexID
  • 1
  • Check the console when you do that. It raises an error "undefined is not a function (evaluating 'e.preventDefault()')". You are right that the dialog does not go away, but not in the way you think :) – mikeazo Mar 06 '15 at 18:17