Ext 源码学习之 Observal:类
Observable故明思义,就是观察者的意思,主要是事件的处理,在java设计模式中,有观察者这一设计模式.观察者的主要思路是,对于某一事件发生变化,能对该事件的变化
做出相应的处理.用代码来来说就是增加事件侦听,和侦听的处理机制.
Observable类在定义时,代码如下:
if(this.listeners){ this.on(this.listeners); delete this.listeners; } addListener : function(eventName, fn, scope, o){ 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"){ ce = new Ext.util.Event(this, eventName); this.events[eventName] = ce; } ce.addListener(fn, scope, o); } Ext.util.Event = function(obj, name){ this.name = name; this.obj = obj; this.listeners = []; }; Ext.util.Event.prototype = { addListener : function(fn, scope, options){ scope = scope || this.obj; if(!this.isListening(fn, scope)){ var l = this.createListener(fn, scope, options); if(!this.firing){ this.listeners.push(l); }else{ // if we are currently firing this event, don't disturb the listener loop this.listeners = this.listeners.slice(0); this.listeners.push(l); } } }, 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){ h = createDelayed(h, o, scope); } if(o.single){ h = createSingle(h, this, fn, scope); } if(o.buffer){ h = createBuffered(h, o, scope); } l.fireFn = h; return l; }, 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; }, 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); this.listeners.splice(index, 1); } return true; } return false; }, clearListeners : function(){ this.listeners = []; }, 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]; if(l.fireFn.apply(l.scope||this.obj||window, arguments) === false){ this.firing = false; return false; } } this.firing = false; } return true; } }; fireEvent : function(){ if(this.eventsSuspended !== true){ var ce = this.events[arguments[0].toLowerCase()]; if(typeof ce == "object"){ return ce.fire.apply(ce, Array.prototype.slice.call(arguments, 1)); } } return true; }person = function(name) { person.superclass.constructor.call(arguments); this.name = name; this.addEvents("update");//增加侦听,可以放在外面处理};Ext.extend(person,Ext.util.Observable,{ setName:function(name){ this.name = name; }, update : function(name){ if(this.hasListener("update")){ this.fireEvent("update",{newName:name,oldName:this.name});//更新时处理 } this.name = name; }}); var p = new person("Jacky"); p.on("update", function(args) { alert(JSON.stringify(args)); }); p.update("Andy"); p.on("update", function(args) { alert("SHIT"); }); if(l.fireFn.apply(l.scope||this.obj||window, arguments) === false){ this.firing = false; return false; }Ext.util.Observable.capture 对原有事件的catch Ext.util.Observable.capture(p,function(fn,option){ alert(JSON.stringify(option)); }) ;