stefangabos / Zebra_Dialog

A small, compact, and highly configurable jQuery plugin for creating beautiful modal dialog boxes
https://stefangabos.github.io/Zebra_Dialog/flat.html
Other
155 stars 67 forks source link

Control whether dialog should close in callback #1

Closed JamesNK closed 11 years ago

JamesNK commented 11 years ago

As far as I can tell right now a dialog always closes when a button is clicked. I'd like a way to control whether it should close or not in the button callback.

e.g.

'buttons': [
    'Cancel',
    {
        caption: "Add",
        callback: function () {
            if (dialogModel.isValid()) {
                return true;
            }
            dialogModel.errors.showAllMessages();
            return false;
        }
    }
]

I made this change to zebra_dialog.js:

// handle the button's click event
button.bind('click', function() {
    var shouldClose = true;
    // execute the callback function when button is clicked
    if (undefined != value.callback) shouldClose = value.callback(plugin.dialog);

    // remove the overlay and the dialog box from the DOM
    // also pass the button's label as argument
    if (shouldClose) plugin.close(undefined != value.caption ? value.caption : value)

});

You might want to modify if so that callbacks that don't return any value still close for backwards compatibility.

stefangabos commented 11 years ago

use jQuery's preventDefault to cancel an event

'buttons': [
    'Cancel',
    {
        caption: "Add",
        callback: function (e) {
            // if don't want to close the dialog box
            // notice the "e" argument to the function
            e.preventDefault();
        }
    }
]