javascript - How can I get User confirm on combobox change event in ExtJs? - Stack Overflow

I have a bo in my extjs application and I want to show 'Are you sure?' confirm window to the

I have a bo in my extjs application and I want to show 'Are you sure?' confirm window to the user and prevent changing if user said no.

Since JavaScript's confirm box is synchronous, it works properly. But using Ext JS, the confirmation message is shown, and the rest of my code would be executed before user responds. Here is my code:

// JavaScript confirm box
{
    xtype: 'bo',
    ...
    ...
    ...
    listeners: {
        beforeselect: function(bo, record, index ) {
            if(confirm('Are you sure ?') == false)
            {
                 return false; // prevent bo from changing
            }
            // else continue
        }
    }
}
// Ext JS message box (to confirm)
{
    xtype: 'bo',
    ...
    ...
    ...
    listeners: {
        beforeselect: function(bo, record, index ) {
            Ext.Msg.show({
                title: 'Warning',
                msg: 'Are You Sure ?',
                buttons: Ext.Msg.YESNO,
                fn: function(btn) {
                    if (btn == 'yes') {
                        // continue and set new value on bo
                    }
                    else if (btn == 'no') {
                        // prevent bo from changing
                    }
                }
            });
        }
    }
}

The problem is the Ext.Msg.show gets a callback function and is not waiting for user answer and we're not able to prevent bobox changing.

What should I do?

I have a bo in my extjs application and I want to show 'Are you sure?' confirm window to the user and prevent changing if user said no.

Since JavaScript's confirm box is synchronous, it works properly. But using Ext JS, the confirmation message is shown, and the rest of my code would be executed before user responds. Here is my code:

// JavaScript confirm box
{
    xtype: 'bo',
    ...
    ...
    ...
    listeners: {
        beforeselect: function(bo, record, index ) {
            if(confirm('Are you sure ?') == false)
            {
                 return false; // prevent bo from changing
            }
            // else continue
        }
    }
}
// Ext JS message box (to confirm)
{
    xtype: 'bo',
    ...
    ...
    ...
    listeners: {
        beforeselect: function(bo, record, index ) {
            Ext.Msg.show({
                title: 'Warning',
                msg: 'Are You Sure ?',
                buttons: Ext.Msg.YESNO,
                fn: function(btn) {
                    if (btn == 'yes') {
                        // continue and set new value on bo
                    }
                    else if (btn == 'no') {
                        // prevent bo from changing
                    }
                }
            });
        }
    }
}

The problem is the Ext.Msg.show gets a callback function and is not waiting for user answer and we're not able to prevent bobox changing.

What should I do?

Share Improve this question asked Mar 2, 2013 at 15:14 Omid ShariatiOmid Shariati 1,9165 gold badges22 silver badges45 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

In order to cancel the bobox change, the beforeSelect listener needs to return false. My suggestion is:

beforeselect: function(bo, record, index ) {
  Ext.Msg.show({
    title: 'Warning',
    msg: 'Are You Sure ?',
    buttons: Ext.Msg.YESNO,
    fn: function(btn) {
      if (btn == 'yes') {

        // Here we have to manually set the bo value
        // since the original select event was cancelled
        bo.setValue( /* whatever value was selected */ );
      }

      else if (btn == 'no') {

        // Don't do anything because the select event was already cancelled
      }
    }
  });

  // Cancel the default action
  return false;
}

The ExtJS Modal does not halt execution of the script like the native dialog, which means that the beforeSelect listener was returning prior to the user action. The way this code works is that the select event is immediately stopped, and the dialog shown. When the user selects "yes", then the value on the bo is programmatically set in the callback function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743729554a4497197.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信