ExtJS MessageBox 不会像alert(..) 那样阻塞

Oli*_*ins 3 extjs extjs4.1

ExtJS MessageBox似乎不像 Javascriptalert(..) 那样阻塞。我想显示一个弹出窗口,然后调用 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();
    }
})
Run Code Online (Sandbox Code Playgroud)

..没有显示弹出窗口,但父窗口已关闭。

如果我使用标准 Javascript警报,则警报将被阻止。单击“确定”按钮后,将执行回调,然后窗口关闭。

    //Alert Box :
    alert('asdf')


    //callback
    Ext.Ajax.request({
        url: someURL,
        method: 'POST',
        callback: function (options, success, response) {
            //do some stuff
            self.up('window').destroy();
        }
    })
Run Code Online (Sandbox Code Playgroud)
  • 为什么MessageBox不阻塞?
  • 我该怎么做才能解决这个问题?
  • MessageBox 是否需要知道要阻止的父窗口?

Ale*_*der 5

它不会阻塞,因为自定义 JavaScript 代码不支持块。正如 Chrome 控制台告诉我们的那样,

window.alert
function alert() { [native code] }
Run Code Online (Sandbox Code Playgroud)

本机代码可以阻止执行。

在 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);
Run Code Online (Sandbox Code Playgroud)

如果此类回调嵌套得很深,我倾向于将回调扁平化,如下所示:

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);
Run Code Online (Sandbox Code Playgroud)

在较新版本的 ExtJS 中,您可以查看Ext.Promise,但在 ExtJS 4.1 中则不行。