`
jljlpch
  • 浏览: 323997 次
  • 性别: Icon_minigender_1
  • 来自: 南昌
社区版块
存档分类
最新评论

Event 分析三:Ext.Util.Observalbe 源码

    博客分类:
  • Ext
阅读更多
/**
 * author:prk
 * date:2008-08-01
 * comment:event analyse.
 * 
 */

/*
 * Ext JS Library 2.0
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

/**
 * @class Ext.util.Observable
 * Abstract base class that provides a common interface for publishing events. Subclasses are expected to
 * to have a property "events" with all the events defined.<br>
 * For example:
 * <pre><code>
 Employee = function(name){
    this.name = name;
    this.addEvents({
        "fired" : true,
        "quit" : true
    });
 }
 Ext.extend(Employee, Ext.util.Observable);
</code></pre>
 */
Ext.util.Observable = function(){
    /**
     * @cfg {Object} listeners A config object containing one or more event handlers to be added to this
     * object during initialization.  This should be a valid listeners config object as specified in the
     * {@link #addListener} example for attaching multiple handlers at once.
     */
	//通过config,把listeners传进来。
    if(this.listeners){
        this.on(this.listeners);
        delete this.listeners;
    }
};
Ext.util.Observable.prototype = {
    /**
     * Fires the specified event with the passed parameters (minus the event name).
     * @param {String} eventName
     * @param {Object...} args Variable number of parameters are passed to handlers
     * @return {Boolean} returns false if any of the handlers return false otherwise it returns true
     */
    fireEvent : function(){
    	//支持全局的挂起事件
        if(this.eventsSuspended !== true){
            var ce = this.events[arguments[0].toLowerCase()];
            //调用Event.fire()
            if(typeof ce == "object"){
                return ce.fire.apply(ce, Array.prototype.slice.call(arguments, 1));
            }
        }
        return true;
    },

    // private
    filterOptRe : /^(?:scope|delay|buffer|single)$/,

    /**
     * Appends an event handler to this component
     * @param {String}   eventName The type of event to listen for
     * @param {Function} handler The method the event invokes
     * @param {Object}   scope (optional) The scope in which to execute the handler
     * function. The handler function's "this" context.
     * @param {Object}   options (optional) An object containing handler configuration
     * properties. This may contain any of the following properties:<ul>
     * <li><b>scope</b> : Object<p class="sub-desc">The scope in which to execute the handler function. The handler function's "this" context.</p></li>
     * <li><b>delay</b> : Number<p class="sub-desc">The number of milliseconds to delay the invocation of the handler after the event fires.</p></li>
     * <li><b>single</b> : Boolean<p class="sub-desc">True to add a handler to handle just the next firing of the event, and then remove itself.</p></li>
     * <li>buffer {Number} Causes the handler to be scheduled to run in an {@link Ext.util.DelayedTask} delayed
     * by the specified number of milliseconds. If the event fires again within that time, the original
     * handler is <em>not</em> invoked, but the new handler is scheduled in its place.</li>
     * </ul><br>
     * <p>
     * <b>Combining Options</b><br>
     * Using the options argument, it is possible to combine different types of listeners:<br>
     * <br>
     * A normalized, delayed, one-time listener that auto stops the event and passes a custom argument (forumId)
     * <pre><code>
el.on('click', this.onClick, this, {
    single: true,
    delay: 100,
    forumId: 4
});</code></pre>
     * <p>
     * <b>Attaching multiple handlers in 1 call</b><br>
      * The method also allows for a single argument to be passed which is a config object containing properties
     * which specify multiple handlers.
     * <p>
     * <pre><code>
foo.on({
    'click' : {
        fn: this.onClick,
        scope: this,
        delay: 100
    },
    'mouseover' : {
        fn: this.onMouseOver,
        scope: this
    },
    'mouseout' : {
        fn: this.onMouseOut,
        scope: this
    }
});</code></pre>
     * <p>
     * Or a shorthand syntax:<br>
     * <pre><code>
foo.on({
    'click' : this.onClick,
    'mouseover' : this.onMouseOver,
    'mouseout' : this.onMouseOut,
     scope: this
});</code></pre>
     */
    addListener : function(eventName, fn, scope, o){
    	//第一个参数是数组,说明有多个事件,采用object的形式来传入的
        if(typeof eventName == "object"){
            o = eventName;
            for(var e in o){
            	// /^(?:scope|delay|buffer|single)$/,
            	// 几个事件共享option,如,scope:this.
                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();
        //如果事件名所对于的事件不存在,就创建再添加this.events
        var ce = this.events[eventName] || true;
        if(typeof ce == "boolean"){
            ce = new Ext.util.Event(this, eventName);
            this.events[eventName] = ce;
        }
        //为该事件添加监听
        ce.addListener(fn, scope, o);
    },

    /**
     * Removes a listener
     * @param {String}   eventName     The type of event to listen for
     * @param {Function} handler        The handler to remove
     * @param {Object}   scope  (optional) The scope (this object) for the handler
     */
    removeListener : function(eventName, fn, scope){
        var ce = this.events[eventName.toLowerCase()];
        if(typeof ce == "object"){
            ce.removeListener(fn, scope);
        }
    },

    /**
     * Removes all listeners for this object
     */
    purgeListeners : function(){
        for(var evt in this.events){
            if(typeof this.events[evt] == "object"){
                 this.events[evt].clearListeners();
            }
        }
    },
    
   /**     
    * @param {} o,addListener的对象。一般为this
    * @param {} events 数组eventname的数组
    */    
    relayEvents : function(o, events){
        var createHandler = function(ename){
            return function(){
                return this.fireEvent.apply(this, Ext.combine(ename, Array.prototype.slice.call(arguments, 0)));
            };
        };
        for(var i = 0, len = events.length; i < len; i++){
            var ename = events[i];
            //add eventname
            if(!this.events[ename]){ this.events[ename] = true; };
            //add listener
            o.on(ename, createHandler(ename), this);
        }
    },

    /**
     * Used to define events on this Observable
     * @param {Object} object The object with the events defined
     */
    //参数的形式一:addEvents('click','dblclick','change')
    //形式二:addEvents({click:true,dblclick: dblclickEvent})
    addEvents : function(o){
    	//new this.events 
        if(!this.events){
            this.events = {};
        }
        //形式一
        if(typeof o == 'string'){
            for(var i = 0, a = arguments, v; v = a[i]; i++){
                if(!this.events[a[i]]){
                    o[a[i]] = true;
                }
            }
        }else{
        	//形式二
            Ext.applyIf(this.events, o);
        }
    },

    /**
     * Checks to see if this object has any listeners for a specified event
     * @param {String} eventName The name of the event to check for
     * @return {Boolean} True if the event is being listened for, else false
     */
    hasListener : function(eventName){
        var e = this.events[eventName];
        return typeof e == "object" && e.listeners.length > 0;
    },

    /**
     * Suspend the firing of all events. (see {@link #resumeEvents})
     */
    suspendEvents : function(){
        this.eventsSuspended = true;
    },

    /**
     * Resume firing events. (see {@link #suspendEvents})
     */
    resumeEvents : function(){
        this.eventsSuspended = false;
    },

    // these are considered experimental
    // allows for easier interceptor and sequences, including cancelling and overwriting the return value of the call
    // private
    getMethodEvent : function(method){
    	
    	//本组件添加methodEvents
        if(!this.methodEvents){
            this.methodEvents = {};
        }
        
        //methodEvents={method:{originalFn:this.method,methodName:method,before:[],ater:[]}}
        //添加一个methodEvents[method]中method,如上面
        var e = this.methodEvents[method];
        if(!e){
            e = {};
            this.methodEvents[method] = e;    
            e.originalFn = this[method];
            e.methodName = method;
            e.before = [];
            e.after = [];


            var returnValue, v, cancel;
            var obj = this;
           
            //{returnValue:xxx,cancel:xxx}    
            //生成一个调用函数
            var makeCall = function(fn, scope, args){
                if((v = fn.apply(scope || obj, args)) !== undefined){
                    if(typeof v === 'object'){
                        if(v.returnValue !== undefined){
                            returnValue = v.returnValue;
                        }else{
                            returnValue = v;
                        }
                        if(v.cancel === true){
                            cancel = true;
                        }
                    }else if(v === false){
                        cancel = true;
                    }else {
                        returnValue = v;
                    }
                }
            }
           //生成本组件的一个方法,this[method],只要调用这个就可以了。
            this[method] = function(){
                returnValue = v = undefined; cancel = false;
                
                //说明拦截的函数和原函数都采用同样的参数。
                var args = Array.prototype.slice.call(arguments, 0);
                
                //运行before中的函数
                for(var i = 0, len = e.before.length; i < len; i++){
                    makeCall(e.before[i].fn, e.before[i].scope, args);
                    if(cancel){
                        return returnValue;
                    }
                }
                //执行原始函数
                if((v = e.originalFn.apply(obj, args)) !== undefined){
                    returnValue = v;
                }
                
                //运行after函数
                for(var i = 0, len = e.after.length; i < len; i++){
                    makeCall(e.after[i].fn, e.after[i].scope, args);
                    if(cancel){
                        return returnValue;
                    }
                }
                return returnValue;
            };
        }
        return e;
    },

    // adds an "interceptor" called before the original method
    beforeMethod : function(method, fn, scope){
        var e = this.getMethodEvent(method);
        e.before.push({fn: fn, scope: scope});
    },

    // adds a "sequence" called after the original method
    afterMethod : function(method, fn, scope){
        var e = this.getMethodEvent(method);
        e.after.push({fn: fn, scope: scope});
    },

    removeMethodListener : function(method, fn, scope){
        var e = this.getMethodEvent(method);
        for(var i = 0, len = e.before.length; i < len; i++){
            if(e.before[i].fn == fn && e.before[i].scope == scope){
                e.before.splice(i, 1);
                return;
            }
        }
        for(var i = 0, len = e.after.length; i < len; i++){
            if(e.after[i].fn == fn && e.after[i].scope == scope){
                e.after.splice(i, 1);
                return;
            }
        }
    }
};
/**
 * Appends an event handler to this element (shorthand for addListener)
 * @param {String}   eventName     The type of event to listen for
 * @param {Function} handler        The method the event invokes
 * @param {Object}   scope (optional) The scope in which to execute the handler
 * function. The handler function's "this" context.
 * @param {Object}   options  (optional)
 * @method
 */
Ext.util.Observable.prototype.on = Ext.util.Observable.prototype.addListener;
/**
 * Removes a listener (shorthand for removeListener)
 * @param {String}   eventName     The type of event to listen for
 * @param {Function} handler        The handler to remove
 * @param {Object}   scope  (optional) The scope (this object) for the handler
 * @method
 */
Ext.util.Observable.prototype.un = Ext.util.Observable.prototype.removeListener;

/**
 * Starts capture on the specified Observable. All events will be passed
 * to the supplied function with the event name + standard signature of the event
 * <b>before</b> the event is fired. If the supplied function returns false,
 * the event will not fire.
 * @param {Observable} o The Observable to capture
 * @param {Function} fn The function to call
 * @param {Object} scope (optional) The scope (this object) for the fn
 * @static
 */
Ext.util.Observable.capture = function(o, fn, scope){
    o.fireEvent = o.fireEvent.createInterceptor(fn, scope);
};

/**
 * Removes <b>all</b> added captures from the Observable.
 * @param {Observable} o The Observable to release
 * @static
 */
Ext.util.Observable.releaseCapture = function(o){
    o.fireEvent = Ext.util.Observable.prototype.fireEvent;
};

(function(){
     //创建在规定多长间隔执行h函数的函数
    var createBuffered = function(h, o, scope){
        var task = new Ext.util.DelayedTask();
        return function(){
            task.delay(o.buffer, h, scope, Array.prototype.slice.call(arguments, 0));
        };
    };
    //一个监听只执行一次,就去掉
    var createSingle = function(h, e, fn, scope){
        return function(){
            e.removeListener(fn, scope);
            return h.apply(scope, arguments);
        };
    };
   
    //创建在规定多长时间之后执行h函数的函数
    var createDelayed = function(h, o, scope){
        return function(){
            var args = Array.prototype.slice.call(arguments, 0);
            setTimeout(function(){
                h.apply(scope, args);
            }, o.delay || 10);
        };
    };

    Ext.util.Event = function(obj, name){
        this.name = name;
        this.obj = obj;
        this.listeners = [];
    };

    Ext.util.Event.prototype = {
    	
    	//为Event增加一个Listener
        addListener : function(fn, scope, options){
            scope = scope || this.obj;
            //从Event中listeners查找有没有在scope中的fn的函数注册,没有
            if(!this.isListening(fn, scope)){
            	
            	//创建一个Listener
                var l = this.createListener(fn, scope, options);
               //存到 this.listeners中
                if(!this.firing){
                    this.listeners.push(l);
                }else{ // if we are currently firing this event, don't disturb the listener loop
                   // this.listeners.slice(0)复制一个娄组。this.listeners现在指向这个数组。
                	//原来的数组还在内存中。.
                	this.listeners = this.listeners.slice(0);
                    this.listeners.push(l);
                }
            }
        },

        //创建一个Listener
        // this.listeners = [{fn: fn, scope: scope, options: o,fireFn:h}]
        createListener : function(fn, scope, o){
            o = o || {};
            scope = scope || this.obj;
            var l = {fn: fn, scope: scope, options: o};
            var h = fn;
            if(o.delay){//delay
                h = createDelayed(h, o, scope);
            }
            if(o.single){//onetime
                h = createSingle(h, this, fn, scope);
            }
            if(o.buffer){//interval
                h = createBuffered(h, o, scope);
            }
            l.fireFn = h;
            return l;
        },
         
          //从Event中listeners查找有没有在scope中的fn的函数注册,返回index
         // this.listeners = [{fn: fn, scope: scope, options: o,fireFn:h}]
        findListener : function(fn, scope){
            scope = scope || this.obj;
            var ls = this.listeners;
            for(var i = 0, len = ls.length; i < len; i++){
                var l = ls[i];
                if(l.fn == fn && l.scope == scope){
                    return i;
                }
            }
            return -1;
        },

        isListening : function(fn, scope){
            return this.findListener(fn, scope) != -1;
        },
         
        //remove 该事件的一个监听函数
        removeListener : function(fn, scope){
            var index;
            if((index = this.findListener(fn, scope)) != -1){
                if(!this.firing){
                    this.listeners.splice(index, 1);
                }else{
                    this.listeners = this.listeners.slice(0);//copy
                    this.listeners.splice(index, 1);//remove
                }
                return true;
            }
            return false;
        },
     
        clearListeners : function(){
            this.listeners = [];
        },
       
        //fire一个事件的所有监听。
        fire : function(){
            var ls = this.listeners, scope, len = ls.length;
            if(len > 0){
                this.firing = true;
                var args = Array.prototype.slice.call(arguments, 0);
                //执行事件中的所有监听函数。
                for(var i = 0; i < len; i++){
                    var l = ls[i];
                    //frieFn
                    if(l.fireFn.apply(l.scope||this.obj||window, arguments) === false){
                        this.firing = false;
                        return false;
                    }
                }
                this.firing = false;
            }
            return true;
        }
    };
})();

 

分享到:
评论
2 楼 xiaofei_suman 2009-08-20  
非常感谢———— 字数够了。。
1 楼 west2504 2009-02-23  
您的博客园的文章:
ExtJS组件事件

链接已经失效?
只能通过快照来访问,着实可惜。

相关推荐

    Tomcat内存溢出的解决方法(java.util.concurrent.ExecutionException)

    "java.util.concurrent.ExecutionException: java.lang.OutOfMemoryError" 是一个典型的错误提示,它表明在并发执行过程中遇到了内存不足的问题。下面我们将深入探讨这个问题的原因、影响以及如何解决。 内存溢出...

    ExtJs4.0 使用心得@1 Ext.util.Format.Number()

    本文将深入探讨Ext.util.Format.Number()函数,它是ExtJS 4.0中的一个重要工具,用于格式化数字。 `Ext.util.Format`是ExtJS中一个非常实用的工具类,包含了一系列用于字符串、日期和数值等类型的数据格式化的静态...

    java.util.ConcurrentModificationException 异常问题详解1

    Java.util.ConcurrentModificationException 异常问题详解 ConcurrentModificationException 异常是 Java 中一个常见的异常,它发生在 Iterator 遍历集合时,集合同时被修改引起的异常。在 Java 中,集合类如 ...

    Ext Js权威指南(.zip.001

    5.3.1 内部事件对象:ext.util.event / 184 5.3.2 为组件添加事件接口:ext.util.observable / 188 5.3.3 为组件绑定事件 / 189 5.3.4 内部事件的触发过程 / 192 5.3.5 移除事件 / 194 5.4 特定功能的事件对象...

    JBuider第七章:Java.util包.rar

    Java.util.concurrent包(虽然不是直接在Java.util下,但密切相关)包含了一系列线程安全的数据结构和并发工具,如Semaphore、ExecutorService、CountDownLatch等,极大地简化了多线程编程。 8. **枚举Set**: ...

    tomcat启动报错:java.util.zip.ZipException的解决方法

    发现问题 早上起来报错误,Jenkins打包到tomcat服务器,死活启动不起来,一些定时任务也没跑成功。 报错如下: org.apache.catalina.startup.ContextConfig.beforeStart ... at java.util.zip.ZipFile.(ZipFi

    extjs帮助文档

    - `Ext.util.TextMetrics.measureText(text, element)`:测量文本的宽度和高度。 #### 二十三、Ext.XTemplate类(第21页) - **概述**:Ext.XTemplate类提供了一个更强大的模板引擎,支持复杂的逻辑。 - **常用...

    常用Extjs工具:Extjs.util.Format使用方法

    抄些常用工具方便查找 ———-字符串 Ext.util.Format.capitalize(string str);//将首字母变大写 Ext.util.Format.ellipsis(string value, Number length);//截取指定length字符,将自动在尾处添加省略号’…’ Ext....

    org.jbundle.util.osgi.wrapped.org.apache.http.client-4.1.2.jar

    org.jbundle.util.osgi.wrapped.org.apache.http.client-4.1.2.jar

    hadoop java.lang.UnsatisfiedLinkError

    解决方案:Exceptionin thread "main" java.lang.UnsatisfiedLinkError:org.apache.hadoop.util.NativeCrc32.nativeCo

    extjs弹出框 n秒后消失

    实现EXTJS弹出框在n秒后自动消失,我们可以利用EXTJS的定时器(Ext.util.DelayedTask)和弹出框(Ext.MessageBox)的配置选项。下面我们将详细讨论如何实现这个功能。 首先,EXTJS的弹出框主要通过`Ext.MessageBox....

    java.util.concurrent.ExecutionException 问题解决方法

    `java.util.concurrent.ExecutionException` 是Java并发编程中一个常见的异常,通常在执行Future对象的get()方法时抛出。这个异常表明在异步任务的执行过程中发生了异常。当我们使用ExecutorService提交任务并尝试...

    java.util.ConcurrentModificationException 解决方法

    java.util.ConcurrentModificationException 解决方法 在使用iterator.hasNext()操作迭代器的时候,如果此时迭代的对象发生改变,比如插入了新数据,或者有数据被删除。 则使用会报以下异常: Java.util....

    StringUtil.java

    字符串工具类,判定字符串是否为空等,封装各种字符串工具方法每个方法都有注释

    Ext 3.0 中文API CHM

    10. **事件系统**:Ext.util.Observable是所有可观察对象的基类,提供了事件监听和触发机制,是实现组件间通信的关键。 11. **国际化支持**:Ext 3.0 提供了本地化支持,允许开发者轻松地将应用翻译成不同语言。 ...

    出现java.util.ConcurrentModificationException 问题及解决办法

    在Java编程中,`java.util.ConcurrentModificationException` 是一个常见的运行时异常,通常发生在尝试并发修改集合时。这个异常的产生是由于集合类(如HashMap)的非线程安全特性,当你在一个线程中使用迭代器遍历...

    java并发工具包 java.util.concurrent中文版用户指南pdf

    1. java.util.concurrent - Java 并发工具包 2. 阻塞队列 BlockingQueue 3. 数组阻塞队列 ArrayBlockingQueue 4. 延迟队列 DelayQueue 5. 链阻塞队列 LinkedBlockingQueue 6. 具有优先级的阻塞队列 ...

    Could not initialize class net.sf.jasperreports.engine.util.JRStyledTextParser

    标题 "Could not initialize class net.sf.jasperreports.engine.util.JRStyledTextParser" 提示了一个Java运行时错误,这通常意味着在尝试加载或初始化`JRStyledTextParser`类时遇到了问题。`JRStyledTextParser`是...

Global site tag (gtag.js) - Google Analytics