生成有吸引力的、用户友好的确认对话框的最简单方法是采用已经编写的内容。
这是基于此处jQuery UI
回答的类似问题的内容。
// static method of jQuery
(function($, $container) {
$.confirm = function(question, title) {
return $.Deferred(function(defer) {
$container.html(question).dialog({
'autoOpen': true,
'modal': true,
'title': title || 'Confirm',
'buttons': {
'Yes': defer.resolve,
'No': defer.reject
},
'close': defer.reject
});
}).always(function() {
$container.dialog('close');
}).promise();
}
}(jQuery, $('<div/>')));
演示
你可以写类似于模仿window.alert()
和 window.prompt()
用法 ...
jQuery.confirm(...)
返回一个承诺,因此你可以写jQuery.confirm(...).then(...)
this.flow.on('fileAdded', (file, event) => {
var that = this;
if (event.type == 'drop') { // File has been dropped
jQuery.confirm('Remove the file?').then(function() {
that.flow.removeFile(file);
}, function() {
console.log('Removal of file not confirmed'); // or whatever you need to do when the action is not confirmed.
});
}
});
不要忘记,$.confirm()
如上所述,它依赖于jQuery UI
它的 CSS 文件。
编辑 1
文档不是很好,flow.js
但表明:
- 在
fileAdded
处理程序中,可以通过返回来拒绝文件false
。
- 可以使用 以编程方式添加文件
Flow.addFile(file)
。
所以有了我jQuery.confirm(...)
的位置(和一些猜测),这里有一些可以在“fileAdded”处理程序中尝试的东西:
this.flow.on('fileAdded', (file, event) => {
var flow = this.flow;
if (event.type == 'drop') {
jQuery.confirm('Remove the file?').then(function() {
console.log('Removal of file confirmed');
// flow.removeFile(file); // having returned false, this shouldn't be necessary.
}, function() {
console.log('Removal of file not confirmed');
flow.addFile(file);
// not sure if the queue will start/resume automatically; if not then try one of the following:
// flow.upload();
// flow.resume();
});
return false; // reject the file
}
return true;
});
笔记:
- 我不能保证这是 100% 正确的。评论指出了不确定性所在。您可能需要运行测试并准备进行一些调试。
flow.addFile()
可能会重新触发“fileAdded”事件,但您的(event.type == 'drop')
测试应该可以防止您陷入无限循环。