`
nieli
  • 浏览: 82842 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Ext.Ajax.request同步请求实现

    博客分类:
  • Ext
阅读更多

Ext.Ajax.request在2.x是异步请求的,这样话在当前请求没有完成之前就不能在它的外部使用它的返回值,在下面这个表单验证中如果不是同步请求的话就有问题了,如:

var text = new Ext.form.TextField({
 fieldLabel : '名称',
 height : 23,
 width : 400,
 id : 'flowName',
 name : 'flowName',
 allowBlank : false,
 blankText : '名称不能为空',
 validateOnBlur : true,
 validationEvent : true,
 validator : function() {
  var resultValue;
  Ext.Ajax.request({
   url : '',
   sync:true,
   params : {
      name : text.getValue()
   },
   success : function(response, options) {
        var responseArray = Ext.util.JSON.decode(response.responseText);
        resultValue=responseArray.resultValue;
   }
  });
  if (resultValue!=null && resultValue!="") {
      text.invalidText = "该名称己经存在,请重新输入!";
   return false;
  } else {
   return true;
  }
 },
 anchor : '95%'
});

 

上面是通过修改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;
        }
    }

    return this["sync" in options ? "syncRequest" : "asyncRequest"](method, uri, cb, data););//这句制定调用的方法,如果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;
    }
};

 

 

 

EXT3.0 同步请求,要倚赖一个JS
下载下面压缩包解压即可,然后引入工程内.
  1. Ext.Ajax.request({   
  2.  async : false,   //ASYNC 是否异步( TRUE 异步 , FALSE 同步)   
  3.  //config 其他参数与以前相同   
  4. }); 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics