需要绕过ExtJS的异步性质

时间:2015-02-26 17:50:13

标签: javascript extjs extjs4 extjs4.1 extjs4.2

我知道Extjs有异步行为,但我仍然需要一种方法来打印console.log('x'+ x的值);作为“x 1的值”

Ext.onReady(function() {
    var x=0;
    Ext.MessageBox.confirm('Confirm', 'Are you sure you want to do that?', showResult );

    function showResult(btn){

            Ext.Msg.show({
                   title: 'number',
                   msg: 'Please enter your number:',
                   width: 300,
                   buttons: Ext.MessageBox.OKCANCEL,
                   multiline: true,
                   fn: saveNumber,
                   icon: Ext.MessageBox.INFO
                });
        function saveNumber(){
            x=1;
        }


    }
    console.log('value of x'+x); //want this value to be 1 without changing the line of this statement
});

1 个答案:

答案 0 :(得分:2)

这是不可能的,你必须使用一个函数来完成你的回调任务。 一个想法是:

Ext.Msg.show({
        title:'Number?',
        message: 'Please enter your number: ',
        buttons: Ext.MessageBox.OKCANCEL,
        multiline: true,
        icon: Ext.MessageBox.INFO,
        fn: function(btn,txt) {
            if(btn==='ok') {
                var number = parseInt(txt,10);
                console.log('Your number is: ' + number);
            }
        }
    }); 

甚至更好:

Ext.Msg.prompt('Number?', 'Please enter your number: ', function(btn,txt){  
        if(btn==='ok') {
            var number = parseInt(txt,10);
            console.log(number);
        }
    });