日历

2009 7.5 Sun
   1234
567891011
12131415161718
19202122232425
262728293031 
«» 2009 - 7 «»

文章搜索

日志文章

2008年02月04日 10:09:23

Ext架构分析(2)--理解Ext.util.Observable

  Observable维护了一个events数组,并提供了更加方便的对于事件的封装和调用机制。同Event一样,它也提供了addListener、removeListener方法。它提供的addListenere方法使用起来更加方便,你可以通过json对象一次实现多个事件的绑定:
foo.addListener({
'click' : {
  fn: this.onClick,
  scope: this,
  delay: 100
},
'mouseover' : {
  fn: this.onMouseOver,
  scope: this
},
'mouseout' : {
  fn: this.onMouseOut,
  scope: this
}
})

如果你看一下源程序,你会发现,实际上,observable最终还是把事件绑定机制委托到Event对象上:
addListener : function(eventName, fn, scope, o){
    //如果参数是一个json对象
    if(typeof eventName == "object"){
    o = eventName;
    for(var e in o){
      if(this.filterOptRe.test(e)){
        continue;
      }
      if(typeof o[e] == "function"){
        // shared options
        this.addListener(e, o[e], o.scope, o);
      }else{
        // individual options
        this.addListener(e, o[e].fn, o[e].scope, o[e]);
      }
    }
    return;
  }
  o = (!o || typeof o == "boolean") ? {} : o;
  eventName = eventName.toLowerCase();
  var ce = this.events[eventName] || true;
  if(typeof ce == "boolean"){
    //事件不存在则新建一个Event对象并把它纳入event数组
    ce = new Ext.util.Event(this, eventName);
    this.events[eventName] = ce;
  }
  //调用event的addListener方法
  ce.addListener(fn, scope, o);
}
除了支持addListener方法,Obserable还提供了一个addEvent方法,通过该方法,Observable可以绑定多个不包含处理句柄的Event对象:
  addEvents : function(o){
    if(!this.events){
        this.events = {};
    }
    if(typeof o == 'string'){
        for(var i = 0, a = arguments, v; v = a; i++){
          if(!this.events[a]){
            o[a] = true;
          }
        }
    }else{
        Ext.applyIf(this.events, o);
    }
  },


为了方便使用,observable对addListener和removeListener提供了一个更加简洁的所写:on和un:
Ext.util.Observable.prototype.on = Ext.util.Observable.prototype.addListener;
Ext.util.Observable.prototype.un = Ext.util.Observable.prototype.removeListener;

你可以通过suspendEvents和resumeEvents方法随时对于事件进行暂停和继续:
suspendEvents : function(){
  this.eventsSuspended = true;
},
resumeEvents : function(){
  this.eventsSuspended = false;
},

当然,通过fireEvent方法,你可以触发制定的事件:
fireEvent : function(){
  if(this.eventsSuspended !== true){
    //arguments[0]就是你需要触发的事件
    var ce = this.events[arguments[0].toLowerCase()];
    if(typeof ce == "object"){
      return ce.fire.apply(ce, Array.prototype.slice.call(arguments, 1));
    }
  }
  return true;
},


Observable还通过capture和releaseCapture提供了对于事件处理函数的拦截机制:
Ext.util.Observable.capture = function(o, fn, scope){
o.fireEvent = o.fireEvent.createInterceptor(fn, scope);
};
Ext.util.Observable.releaseCapture = function(o){
o.fireEvent = Ext.util.Observable.prototype.fireEvent;
};


Tags: Observable   Ext  

类别: Ext |  评论(4) |  浏览(2841) |  收藏
4楼 [匿名]9izwot5q 2009年06月19日 15:51:28 Says:
%E7%BD%91%E7%AB%99%E6%8E%A8%E5%B9%BF%E8%BD%AF%E4%BB%B6,%E5%8D%9A%E5%AE%A2%E7%BE%A4%E5%8F%91%E8%BD%AF%E4%BB%B6,%E5%8D%9A%E5%AE%A2%E7%BE%A4%E5%BB%BA%E8%BD%AF%E4%BB%B6,%E7%BD%91%E7%BB%9C%E8%90%A5%E9%94
3楼 [匿名]gcusujck 2008年08月20日 13:40:05 Says:
%5Bb%5D%5Bsize=3%5D%E9%BE%99%E5%85%B4%E7%BD%91%E7%BB%9C%E6%98%AF%E5%9B%BD%E5%86%85%E4%B8
2楼 [匿名]小石 2008年04月24日 14:33:39 Says:
很好
正在读代码!
1楼 [匿名]haodaluobo 2008年04月02日 14:57:19 Says:
addEvents : function(o){
  if(!this.events){
    this.events = {};
  }
  if(typeof o == 'string'){
    for(var i = 0, a = arguments, v; v = a; i++){
      if(!this.events[a]){
        o[a] = true;
      }
    }
  }else{
    Ext.applyIf(this.events, o);
  }
},

中间部分的for循环处,抄错了.
而且原来的代码好像也是出错了,没有任何实际意义.
而最后的地方Ext.apply这个,个人认为更是设计上的错误,本来addListener内部实现了比较复杂的算法,包装了Event(Event类从设计上看还是个内部类),但是这里却要直接的灌入,破坏了设计思路.
发表评论
看不清楚,换一张