Ajax::prototype 源码解读

2019-06-03 08:36:20刘景俊

        this.transport.setRequestHeader('Connection', 'close'); 
        this.transport.setRequestHeader('Content-type', 
          'application/x-www-form-urlencoded'); 
      } 

      this.transport.send(this.options.method == 'post' ? 
        this.options.parameters + '&_=' : null); 

    } catch (e) { 
    }    
  }, 

  onStateChange: function() { 
    var readyState = this.transport.readyState; 
   /** 
    * 如果不是 Loading 状态,就调用回调函数 
     */ 
    if (readyState != 1) 
      this.respondToReadyState(this.transport.readyState); 
  }, 

  /** 
   * 回调函数定义在 this.options 属性中,比如: 
      var option = { 
         onLoaded : function(req) {...}; 
         ...... 
      } 
      new Ajax.Request(url, option); 
   */ 
  respondToReadyState: function(readyState) { 
    var event = Ajax.Request.Events[readyState]; 
    (this.options['on' + event] || Ajax.emptyFunction)(this.transport); 
  } 
}); 

/** 
 * Ajax.Updater 用于绑定一个html元素与 XmlHttp调用的返回值。类似与 buffalo 的 bind。 
 * 如果 options 中有 insertion(from dom.js) 对象的话, insertion 能提供更多的插入控制。 
 */ 
Ajax.Updater = Class.create(); 
Ajax.Updater.prototype = (new Ajax.Base()).extend({ 
  initialize: function(container, url, options) { 
    this.container = $(container); 
    this.setOptions(options); 

    if (this.options.asynchronous) { 
      this.onComplete = this.options.onComplete; 
      this.options.onComplete = this.updateContent.bind(this);