Ext.Ajax.request的同步请求实现

开发者博客:www.developsearch.com

Extjs2.x   实现同步方法
Ext.Ajax.request实现同步请求是通过修改ext-base.js中的Ext.lib.Ajax.request来实现同步请求:

/**
Adding a synchronous request to the Ext asynchronous only mode of operation.

History: coded from Ext 2.2.

Additional configs.

@param {Object} options
@config {Mixed} [sync] include this for a synchronous request
*/
Ext.lib.Ajax.request = function(method, uri, cb, data, options) {
    if(options){
        var hs = options.headers;
        if(hs){
            for(var h in hs){
                if(hs.hasOwnProperty(h)){
                    this.initHeader(h, hs[h], false);
                }
            }
        }
        if(options.xmlData){
            if (!hs || !hs['Content-Type']){
                this.initHeader('Content-Type', 'text/xml', false);
            }
            method = (method ? method : (options.method ? options.method : 'POST'));
            data = options.xmlData;
        }else if(options.jsonData){
            if (!hs || !hs['Content-Type']){
                this.initHeader('Content-Type', 'application/json', false);
            }
            method = (method ? method : (options.method ? options.method : 'POST'));
            data = typeof options.jsonData == 'object' ? Ext.encode(options.jsonData) : options.jsonData;
        }
    }
 [color=red]return this["sync" in options ? "syncRequest" : "asyncRequest"](method, uri, cb, data););[/color]//这句制定调用的方法,如果sync传递了就调用syncRequest, 否则调用原来的方法asyncRequest};

};

 把下面这个方法加上,直接加在asyncRequest方法后面就可以,形式和asyncRequest相同,调用时如果需要同步调用加上sync:true,属性即可

/**
Synchronous request.  
  
@param {Object} method
@param {Object} uri
@param {Object} callback
@param {Object} postData
*/
Ext.lib.Ajax.syncRequest = function(method, uri, callback, postData)
{
    var o = this.getConnectionObject();

    if (!o) {
        return null;
    }
    else {
        o.conn.open(method, uri, false);

        if (this.useDefaultXhrHeader) {
            if (!this.defaultHeaders['X-Requested-With']) {
                this.initHeader('X-Requested-With', this.defaultXhrHeader, true);
            }
        }

        if(postData && this.useDefaultHeader && (!this.hasHeaders || !this.headers['Content-Type'])){
            this.initHeader('Content-Type', this.defaultPostHeader);
        }

        if (this.hasDefaultHeaders || this.hasHeaders) {
            this.setHeader(o);
        }

        o.conn.send(postData || null);
        this.handleTransactionResponse(o, callback);
        return o;
    }
};

 extjs3.0 实现同步方法
下载ext-basex.rar,解压,引入工程,在 Ext.Ajax.request方法中加入

async : false,   //ASYNC 是否异步( TRUE 异步 FALSE 同步)

相关推荐