Ajax::prototype 源码解读

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


/** 
 * 这个设计和 PeriodicalExecuter 一样,bind 方法是实现的核心 
 */ 
Abstract.TimedObserver.prototype = { 
  initialize: function(element, frequency, callback) { 
    this.frequency = frequency; 
    this.element   = $(element); 
    this.callback  = callback; 

    this.lastValue = this.getValue(); 
    this.registerCallback(); 
  }, 

  registerCallback: function() { 
    setTimeout(this.onTimerEvent.bind(this), this.frequency * 1000); 
  }, 

  onTimerEvent: function() { 
    var value = this.getValue(); 
    if (this.lastValue != value) { 
      this.callback(this.element, value); 
      this.lastValue = value; 
    } 

    this.registerCallback(); 
  } 


/** 
 * Form.Element.Observer 和 Form.Observer 其实是一样的 
 * 注意 Form.Observer 并不是用来跟踪整个表单的,我想大概只是为了减少书写(这是Ruby的一个设计原则) 
 */ 
Form.Element.Observer = Class.create(); 
Form.Element.Observer.prototype = (new Abstract.TimedObserver()).extend({ 
  getValue: function() { 
    return Form.Element.getValue(this.element); 
  } 
}); 

Form.Observer = Class.create(); 
Form.Observer.prototype = (new Abstract.TimedObserver()).extend({ 
  getValue: function() { 
    return Form.serialize(this.element); 
  } 
});
Ajax::prototype 源码解读 之 prototype.js 四[转载] 
/** 
 * 根据 class attribute 的名字得到对象数组,支持 multiple class 
 * 
 */ 
document.getElementsByClassName = function(className) { 
  var children = document.getElementsByTagName('*') || document.all; 
  var elements = new Array(); 

  for (var i = 0; i < children.length; i++) { 
    var child = children[i];