user3546785
user3546785

Reputation: 177

Extjs : Need a confirmation message before closing the window

I have a Ext.window and I would like to display a confirmation message before a user tries to close the window either by clicking the close button in the window or the default [x] button in the top or by Esc key. I have the following code for this purpose.

              closeWindow: function(){
                       Ext.Msg.show({
                      title: 'Confirm or Cancel',
                      msg: 'Are you sure you want to close this window?',
                      buttonText: {ok: 'Yes', cancel: 'No'},
                      icon: Ext.Msg.WARNING,
                      scope: this,
                      width: 400,
                      fn: function(btn, text){
                      if (btn == 'ok'){
                      clearInterval(this.myIntervalTimer);
                      this.cleanEvents(null, null, this.identifier);
                       this.view.destroy();
                    } else {
                   return;
                }
                }
             });
           }

This piece of code works fine when I click on the Close button in the window. But when I click the default close[x] or the esc key, due to async nature of extjs the window disappears before the confirmation message gets displayed. When the confirmation message displays there is no window and so it is becoming irrelevant. I tried using the beforeClose event as well but of no use.

Any suggestions or ideas of how this can be achieved in extjs?. Thanks...

Upvotes: 3

Views: 4261

Answers (1)

Saki
Saki

Reputation: 5856

Test the following please, it works for me:

    Ext.create('Ext.window.Window', {
         title:'Test'
        ,width:400
        ,height:300
        ,autoShow:true
        ,listeners:{
            beforeclose:function(win) {
                if(win.closeMe) {
                    win.closeMe = false;
                    return true;
                }
                Ext.Msg.show({
                     title:'Close confirmation'
                    ,msg:'Really close?'
                    ,buttons:Ext.Msg.YESNO
                    ,callback:function(btn) {
                        if('yes' === btn) {
                            win.closeMe = true;
                            win.close();
                        }
                    }
                })
                return false;
            }
        }
    });

Upvotes: 4

Related Questions