Flex 事件分发(FlexViewer事件机制)剥离过程
将FlexViewer里面的事件分发及监听事件机制剥离出来在其他项目中使用
AppEvent.as
packagecom
{
importflash.events.Event;
/**
*@authorSamSung
*创建时间:2014-7-24下午1:21:05
*
*/
publicclassAppEventextendsEvent
{
//--------------------------------------------------------------------------
//
//Properties
//
//--------------------------------------------------------------------------
privatevar_data:Object;
privatevar_callback:Function;
publicfunctionAppEvent(type:String,data:Object=null,callback:Function=null)
{
super(type);
_data=data;
_callback=callback;
}
/**
*Thedatawillbepassedviatheevent.Itallowstheeventdispatchertopublish
*datatoeventlistener(s).
*/
publicfunctiongetdata():Object
{
return_data;
}
/**
*@private
*/
publicfunctionsetdata(value:Object):void
{
_data=value;
}
/**
*Thecallbackfunctionassociatedwiththisevent.
*/
publicfunctiongetcallback():Function
{
return_callback;
}
/**
*@private
*/
publicfunctionsetcallback(value:Function):void
{
_callback=value;
}
/**
*Overrideclone
*/
publicoverridefunctionclone():Event
{
returnnewAppEvent(this.type,this.data,this.callback);
}
/**
*Dispatchthisevent.
*/
publicfunctiondispatch():Boolean
{
returnEventBus.instance.dispatchEvent(this);
}
/**
*DispatchanAppEventforspecifiedtypeandwithoptionaldataandcallbackreference.
*/
publicstaticfunctiondispatch(type:String,data:Object=null,callback:Function=null):Boolean
{
returnEventBus.instance.dispatchEvent(newAppEvent(type,data,callback));
}
publicstaticfunctionaddListener(type:String,listener:Function,useCapture:Boolean=false,priority:int=0,useWeakReference:Boolean=false):void
{
EventBus.instance.addEventListener(type,listener,useCapture,priority,useWeakReference);
}
publicstaticfunctionremoveListener(type:String,listener:Function,useCapture:Boolean=false):void
{
EventBus.instance.removeEventListener(type,listener,useCapture);
}
}
}
EventBus.as
packagecom
{
importflash.events.Event;
importflash.events.EventDispatcher;
/**
*TheEventBusallowscentrallizedcommunicationamongmoduleswithout
*point-to-pointmessaging.Itusesthesingletondesignpattern
*tomakesureoneeventbusisavailableglobally.Thebusitself
*isonlyavailabletothecontainer.Modulesusethecontainer's
*staticmethodtocommunicatewiththeeventbus.
*/
publicclassEventBusextendsEventDispatcher
{
/**Applicationeventbusinstance*/
publicstaticconstinstance:EventBus=newEventBus();
/**
*NormallytheEventBusisnotinstantiatedviathe<b>new</b>methoddirectly.
*TheconstructorhelpsenforceonlyoneEvenBusavailiablefortheapplication
*(singeton)sothatitasuresthecommunicationonlyviaasigleeventbus.
*/
publicfunctionEventBus()
{
}
/**
*ThefactorymethodisusedtocreateainstanceoftheEventBus.Itreturns
*theonlyinstanaceofEventBusandmakessurenoanotherinstanceiscreated.
*/
[Deprecated(replacement="instance")]
publicstaticfunctiongetInstance():EventBus
{
returninstance;
}
/**
*Basicdispatchfunction,dispatchessimplenamedevents.Inthecase
*thattheeventisonlysignificantbytheeventtoken(typestring),
*thisnewdispatchmethodsimplifythecode.
*/
[Deprecated(replacement="AppEvent.dispatch")]
publicfunctiondispatch(type:String):Boolean
{
returndispatchEvent(newEvent(type));
}
}
}