2013-01-09 111 views
1

我正在使用Extjs版本4.0.7开发项目,并且遇到问题。 我有一个功能,在保存前验证数据有效性。它看起来像这样:Extjs/Javascript - 在函数返回之前等待MessageBox响应

 me.validateEmployeeSystemSettingsOnSave = function (a,b,c) { 
      switch (c) { 
     case 1:{ 
     if(expression) 
      return {success: false} 
      if(expression2){ 
      Ext.Msg.show({ 
       title:'', 
        msg:'', 
       buttons: Ext.Msg.YESNO, 
       icon: Ext.Msg.QUESTION, 
       fn: function (btn) { 
       if (btn == 'yes') { 
       return { success: true }; 
       } 
       else if (btn == 'no') { 
       return { success: false}; 
       } 
       }, 
       modal: true 
       }); 
      } 

     else return {success: true}; /*problem line*/ //this is the default answer, when function will 
//not enter any expression and return {success: true} 

    } 
    case 2:{ 
    } 
    } 

我上面的函数被调用的函数的onSave像这样:

var response = me.validateEmployeeSystemSettingsOnSave(dsa,ssa,daa){ 
    if(response.success){ 
this.save(); 
} 
else{ 
} 
} 

我的问题是: 有没有办法等待Ext.msg的答案.show(),在达到默认return之前,其中我标有/ 问题行 /? 因为现在这个函数并没有采用我的是或否的答案,它总是返回{success: 'true'},这是正常的,考虑到函数结构如何。

任何想法?谢谢!

+0

你已经显示消息框上else if(expression2),所以当消息显示时,else return {success:true};条件不会执行。 –

+0

你说得对,我在这里错了,但在我的代码不是。 – darkdante

回答

3

你永远不应该等待,而是使用回调!因此,在你的情况下执行回调,而不是返回{ success: true };

+0

对于答案,莎拉感谢你。我对javacsript有点新,而且我刚开始学习回调。你能指点我在哪里实施它,如果可能的话,怎么样。 Thanq你!:) – darkdante

相关问题