zoukankan      html  css  js  c++  java
  • FMS服务器端最简单的EventDispatcher类 转

    参考了as2时代的GEventDispatcher类,改写成SSAS,并且精简了一下只提供3个最基本的功能

    1. addEventListener( eventType:String , listener:object , listenerHandler:String )
    2. removeEventListener(eventType:String , listener:object , listenerHandler:String )
    3. dispatchEvent({type:"type"})

    演示代码:
    //需要用到的2个类
    AAA = function(){}
    BBB = function(){}

    //首先加载EventDispatcher
    load("N2EventDispatcher.asc")

    application.onAppStart = function(){
      
      //a1是AAA类的一个实例
      var a1 = new AAA()
      //把a1变成广播者后,a1就可以广播事件了
      N2EventDispatcher.init(a1)
      
      
      //b1是BBB类的一个实例 ,他是一个监听者,监听a1广播的test事件
     var b1 = new BBB() //listener
      
     b1.onTest= function(eo /* :Event */){
       trace("收到"+eo.type +"事件")
      }
      
      a1.addEventListener("test",b1,"onTest") //参数为 (eventType , 监听者,监听者的handler)
      a1.dispatchEvent({type:"test"}) //广播事件,相当于 a1.dispatchEvent(new Event("test"))
      
      a1.removeEventListener("test",b1,"onTest") //删除监听
      a1.dispatchEvent({type:"test"}) //再广播就不会触发了
      
      
    }

    N2EventDispatcher 源代码:

    try { var dummy = N2EventDispatcher; } catch ( e ) { // #ifndef N2VideoPlayback
     
     N2EventDispatcher = function(){}

     N2EventDispatcher.instance = undefined
     N2EventDispatcher.prototype._listeners = undefined
     

     N2EventDispatcher.prototype.init = function(p_obj)
     {
        if (N2EventDispatcher.instance == undefined)
        {
         N2EventDispatcher.instance = new N2EventDispatcher()
        }
        p_obj.dispatchEvent = N2EventDispatcher.instance.dispatchEvent;
        p_obj.addEventListener = N2EventDispatcher.instance.addEventListener;
        p_obj.removeEventListener = N2EventDispatcher.instance.removeEventListener;
        
      }
      
      
      
      
     N2EventDispatcher.prototype.addEventListener = function(p_eventType/* :String */,p_obj /*:Object*/,p_function/*String*/)
     {
        if (this._listeners == undefined)
        {
          this._listeners = {};
        }
        var listeners/* :Array */ = this._listeners[p_eventType];
        if (listeners == undefined)
        {
         this._listeners[p_eventType] = listeners = [];
        }
        
        if (N2EventDispatcher.indexOfListener(listeners,p_obj,p_function) == -1)
        {
          listeners.push({o:p_obj,f:p_function});
        }
      }
      
      N2EventDispatcher.prototype.removeEventListener = function (p_eventType /*:String*/,p_obj /*:Object*/,p_function /*:String*/) /*:Void*/
      {
        var listeners = this._listeners[p_eventType];
        if (listeners == undefined) { return; }
        var index /*:Number*/ = N2EventDispatcher.indexOfListener(listeners,p_obj,p_function);
        if (index != -1) { listeners.splice(index,1); }
      }
      
      N2EventDispatcher.prototype.dispatchEvent = function(p_eventObj)
      {
     if(this._listeners == undefined) return
        if (p_eventObj.target == undefined) { p_eventObj.target = this; }
        var arr = this._listeners[p_eventObj.type]
        if (arr != undefined)
        {
          for( i in arr){
            var scrope = arr[i].o
            var fun = arr[i].f
             scrope[fun](p_eventObj)
            }
        }
        
      }
      
      
      N2EventDispatcher.indexOfListener = function(p_listeners /*:Array*/ , p_obj/*:Object*/,p_function /*:String*/) /*:Number*/
     {
        var l /*:Number*/ = p_listeners.length;
        var i /*:Number*/ = -1;
        while (++i < l) {
          var obj /*:Object*/ = p_listeners[i];
          if (obj.o == p_obj && obj.f == p_function) { return i; }
        }
        return -1;
      }
      
     N2EventDispatcher.init = N2EventDispatcher.prototype.init

    }
  • 相关阅读:
    SpannaleString总结
    【未完成】bug记录2013427>import工程时出现Build path contains duplicate entry:'src' for project 'XXX'
    【未完成】给eclipse项目改名
    android创建和删除桌面快捷方式
    bug记录2013426(2)>Select at least one project错误
    hosts配置
    获取当前应用的版本号及android系统版本号及手机型号
    转载:如何将offcie 2003文档(.doc、.xls、.ppt)转换成mht文档
    转载:.NET2.0 验证控件常用的正则表达式
    转载: RESTORE DATABASE命令还原SQLServer 2005 数据库
  • 原文地址:https://www.cnblogs.com/appleseed/p/1292248.html
Copyright © 2011-2022 走看看