package com.loaderProxy
{
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.HTTPStatusEvent;
import flash.events.IEventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.ProgressEvent;
import flash.events.SecurityErrorEvent;
public class EventListenerProxy extends EventDispatcher
{
public function EventListenerProxy(target:IEventDispatcher=null)
{
super(target);
}
//event handel method
public var completeHandler:Function = function(event:Event):void {};
public var openHandler:Function = function(event:Event):void{};
public var progressHandler:Function = function(event:ProgressEvent):void{};
public var securityErrorHandler:Function = function(event:SecurityErrorEvent):void{};
public var httpStatusHandler:Function = function(event:HTTPStatusEvent):void{};
public var ioErrorHandler:Function = function(event:IOErrorEvent):void{};
public function addLoaderEventListener(target:IEventDispatcher):void
{
addEventListenerProxy(target , Event.COMPLETE ,completeHandler);
addEventListenerProxy(target , Event.OPEN, openHandler);
addEventListenerProxy(target , ProgressEvent.PROGRESS, progressHandler);
addEventListenerProxy(target , SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
addEventListenerProxy(target , HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
addEventListenerProxy(target , IOErrorEvent.IO_ERROR, ioErrorHandler);
}
public function removeLoaderEventListener(target:IEventDispatcher):void
{
removeEventListenerProxy(target , Event.COMPLETE ,completeHandler);
removeEventListenerProxy(target , Event.OPEN, openHandler);
removeEventListenerProxy(target , ProgressEvent.PROGRESS, progressHandler);
removeEventListenerProxy(target , SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
removeEventListenerProxy(target , HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
removeEventListenerProxy(target , IOErrorEvent.IO_ERROR, ioErrorHandler);
}
//EventListener Proxy
public function addEventListenerProxy(target:IEventDispatcher , type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
{
target.addEventListener(type, listener, useCapture, priority, useWeakReference);
}
public function removeEventListenerProxy(target:IEventDispatcher , type:String, listener:Function, useCapture:Boolean = false):void
{
target.removeEventListener(type , listener , useCapture );
}
}
}
而更好的話將event handel method所有的method宣告到一個Interface,如此若是需要不同動作處理者,可實作該Interface來實現。
因此可以:
public var handel:IHandelMethod = new LoaderHandelMethod();
addEventListenerProxy(target , Event.COMPLETE ,handel.completeHandler);
如此程式碼可以簡化並能鬆綁個物件間之關係
沒有留言:
張貼留言