ExtJS MessageBox 似乎不像 Javascript alert(..) 那样阻塞。我想显示一个弹出窗口,然后调用 AJAX 调用,然后关闭窗口。
如果我这样调用 show 方法,那么......
//Alert Box :
var alertBox = Ext.create('Ext.window.MessageBox');
var config = {
title : 'Title',
closable: true,
msg: 'Message',
buttons: Ext.Msg.OK,
buttonText: { ok: EML.lang.buttons.ok },
modal: true
};
alertBox.show(config);
//callback
Ext.Ajax.request({
url: someURL,
method: 'POST',
callback: function (options, success, response) {
//do some stuff
self.up('window').destroy();
}
})
..不显示弹出窗口,但父窗口关闭。
如果我使用标准的 Javascript alert,那么警报将被阻止。单击确定按钮后,将执行回调,然后关闭窗口。
//Alert Box :
alert('asdf')
//callback
Ext.Ajax.request({
url: someURL,
method: 'POST',
callback: function (options, success, response) {
//do some stuff
self.up('window').destroy();
}
})
- 为什么 MessageBox 不阻塞?
- 我该怎么做才能解决这个问题?
- MessageBox 是否需要知道要阻止的父窗口?
最佳答案
它不会阻塞,因为自定义 javascript 代码不支持 block 。正如 Chrome 控制台告诉我们的那样,
window.alert
function alert() { [native code] }
并且 native 代码可以阻止执行。
在 ExtJS 中,您可以像这样为消息框编写回调:
//Alert Box :
var alertBox = Ext.create('Ext.window.MessageBox');
var config = {
title : 'Title',
closable: true,
msg: 'Message',
buttons: Ext.Msg.OK,
buttonText: { ok: EML.lang.buttons.ok },
modal: true,
callback:function(btn) {
//callback
Ext.Ajax.request({
url: someURL,
method: 'POST',
callback: function (options, success, response) {
//do some stuff
self.up('window').destroy();
}
})
}
};
alertBox.show(config);
如果这样的回调嵌套很深,我倾向于像这样扁平化回调:
var store = me.down('grid').getStore();
var callback3 = function(btn) {
if(btn=="yes") store.sync();
};
var callback2 = function() {
Ext.Msg.prompt('A','Third', callback3);
};
var callback1 = function() {
Ext.Msg.alert('A','Second', callback2);
};
Ext.Msg.alert('A','First', callback1);
在较新版本的 ExtJS 中,您可以查看 Ext.Promise
,但在 ExtJS 4.1 中没有。
关于ExtJS MessageBox 不像 alert(..) 那样阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42415984/