【发布时间】:2019-02-21 13:30:18
【问题描述】:
我正在使用 AlertifyJS 来显示自定义表单并调用以更改多条记录。 我已经定义了一个显示对话框的函数:
function showDialog(title, formDialog, callbackfunction, params) {
alertify.dialog('customModal', function factory() {
var placeholder = null
return {
main: function (content) {
if (content instanceof HTMLElement && content.parentNode) {
placeholder = placeholder || document.createComment('')
content.parentNode.insertBefore(placeholder, content)
}
this.setContent(content);
},
setup: function () {
return {
/* buttons collection */
buttons: [
/*button defintion*/
{
/* button label */
text: 'OK',
/*bind a keyboard key to the button */
key: 27,
/* indicate if closing the dialog should trigger this button action */
invokeOnClose: false,
/* custom button class name */
className: alertify.defaults.theme.ok,
/* custom button attributes */
//attrs: { buttonValue: 'submit' },
/* Defines the button scope, either primary (default) or auxiliary */
scope: 'auxiliary',
/* The will conatin the button DOMElement once buttons are created */
element: undefined
},
{
/* button label */
text: 'Cancel',
/*bind a keyboard key to the button */
//key: 27,
/* indicate if closing the dialog should trigger this button action */
invokeOnClose: false,
/* custom button class name */
className: alertify.defaults.theme.cancel,
/* custom button attributes */
//attrs: { buttonValue: 'submit' },
/* Defines the button scope, either primary (default) or auxiliary */
scope: 'auxiliary',
/* The will conatin the button DOMElement once buttons are created */
element: undefined
}
],
options: {
basic: false,
maximizable: false,
resizable: false,
padding: false,
closableByDimmer: false,
title: 'My custom dialog'
}
};
},
callback: function (closeEvent) {
//The closeEvent has the following properties
//
// index: The index of the button triggering the event.
// button: The button definition object.
// cancel: When set true, prevent the dialog from closing.
console.log(closeEvent);
if (closeEvent.index == 0) { //OK Button
callbackfunction(params);
}
},
hooks: {
onclose: function () {
if (placeholder != null) {
var node = this.elements.content.firstElementChild
node.style.display = 'none'
placeholder.parentNode.insertBefore(node, placeholder)
}
}
}
};
});
alertify.customModal($(formDialog)[0]).set('title', title);
$(formDialog).show();
}
此函数会导致“alertify.dialog:名称已存在”的问题。我只是将对话框声明移到了该函数之外的 document.ready 函数中,但我不知道如何传递回调函数:
$(document).ready(function () {
alertify.dialog('customModal', function factory() {
var placeholder = null
return {
main: function (content) {
if (content instanceof HTMLElement && content.parentNode) {
placeholder = placeholder || document.createComment('')
content.parentNode.insertBefore(placeholder, content)
}
this.setContent(content);
},
setup: function () {
return {
/* buttons collection */
buttons: [
/*button defintion*/
{
/* button label */
text: 'OK',
/*bind a keyboard key to the button */
key: 27,
/* indicate if closing the dialog should trigger this button action */
invokeOnClose: false,
/* custom button class name */
className: alertify.defaults.theme.ok,
/* custom button attributes */
//attrs: { buttonValue: 'submit' },
/* Defines the button scope, either primary (default) or auxiliary */
scope: 'auxiliary',
/* The will conatin the button DOMElement once buttons are created */
element: undefined
},
{
/* button label */
text: 'Cancel',
/*bind a keyboard key to the button */
//key: 27,
/* indicate if closing the dialog should trigger this button action */
invokeOnClose: false,
/* custom button class name */
className: alertify.defaults.theme.cancel,
/* custom button attributes */
//attrs: { buttonValue: 'submit' },
/* Defines the button scope, either primary (default) or auxiliary */
scope: 'auxiliary',
/* The will conatin the button DOMElement once buttons are created */
element: undefined
}
],
options: {
basic: false,
maximizable: false,
resizable: false,
padding: false,
closableByDimmer: false,
title: 'My custom dialog'
}
};
},
callback: function (closeEvent) {
//The closeEvent has the following properties
//
// index: The index of the button triggering the event.
// button: The button definition object.
// cancel: When set true, prevent the dialog from closing.
console.log(closeEvent);
if (closeEvent.index == 0) { //OK Button
callbackfunction(params);
}
},
hooks: {
onclose: function () {
if (placeholder != null) {
var node = this.elements.content.firstElementChild
node.style.display = 'none'
placeholder.parentNode.insertBefore(node, placeholder)
}
}
}
};
});
});
function showDialog(title, formDialog, callbackfunction, params) {
alertify.customModal($(formDialog)[0]).set('title', title); //pass callback function here
$(formDialog).show();
}
谢谢
【问题讨论】:
-
在 alertify.dialog('customModal'.... 之前添加 if(!alertify.customModal){ 就足够了。
-
感谢您的快速评论,但是多个回调函数呢?在这种情况下,无论参数如何,都会保留第一个回调。
-
我刚刚测试了使用 setTimeout((function () { alertify.customModal().destroy(); }), 400);最后onclose无济于事。它只是不断调用相同的回调函数
标签: jquery modal-dialog alertifyjs