Control whether dialog should close in callback
JamesNK opened this issue · comments
James Newton-King commented
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.
Stefan Gabos commented
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();
}
}
]