Extjs4,等待ajax请求

时间:2013-03-27 11:09:15

标签: extjs4 wait ajax-request

我应该在一次按钮单击中运行多个ajax请求,但所有请求应该等到第一个请求执行。我试图将所有请求放在第一个请求的成功回调中,但这会产生此错误:

  

TypeError:o未定义
  返回o.id;

只执行第一个请求 这是我的代码:

    if(form1.isValid()) {
                form1.submit(me._genFormSubmitAction('my_DB','my_Action', function() {
                    console.log('form1 success');
                    //Submit Form2

                    if(form2.isValid()) {
                        form2.submit(me._genFormSubmitAction('my_DB','my_Action', function() {
                        console.log('form2 success');
}));

//Submit Form3  
.... 

_genFormSubmitAction:

_genFormSubmitAction: function(db,action, successCallback) {
    var me = this;
    return {
        clientValidation : true,
        url              : me.getApplication().apiUrl,
        waitMsg : '<p align=right>..الرجاء الإنتظار</p>',
        async:false,
        params           : {
            _module: 'administrationcassocial',
            _action: action,
            _db:db
        },
        success          : function(form, action) {
            if(action.result.success == true) {
                Ext.callback(successCallback, me);
                form.owner.destroy();
            } else {
                console.log('url=',url);
                Ext.Msg.alert(action.result.error, action.result.errormessages.join("\n"));
            }
        },
        failure          : function(form, action) {
            switch (action.failureType) {
                case Ext.form.action.Action.CLIENT_INVALID:
                    Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                    break;
                case Ext.form.action.Action.CONNECT_FAILURE:
                    Ext.Msg.alert('Failure', 'Ajax communication failed');
                    break;
                case Ext.form.action.Action.SERVER_INVALID:
                    Ext.Msg.alert(action.result.error, action.result.errormessages.join("\n"));
            }
        }
    };
}

1 个答案:

答案 0 :(得分:0)

这是一个范围问题。

form1.submit的回调发生在回调自己的范围内,所以它不知道form2是什么。

您可以尝试:

if(form1.isValid()) {
    var me = this;
    form1.submit(me._genFormSubmitAction('my_DB','my_Action', function() {
        console.log('form1 success');

        //Submit Form2
        if( me.form2.isValid() ) {
            form2.submit(me._genFormSubmitAction('my_DB','my_Action', function() {
                console.log('form2 success');
            }));
        }
    }));
}

或者在我看来更合适的解决方案:

// Added aScope var
_genFormSubmitAction: function( db,action, aScope, successCallback ) {
    var me = this;
    return {    
        // ...
        scope: aScope
    }
}

然后你打电话:

form1.submit(me._genFormSubmitAction('my_DB','my_Action', this, function() {
}));