将flexviewer里面的事件分发及监听事件机制剥离出来在其他项目中使用
appevent.as
package com
{
import flash.events.event;
/**
* @author samsung
* 创建时间:2014-7-24 下午1:21:05
*
*/
public class appevent extends event
{
//--------------------------------------------------------------------------
//
// properties
//
//--------------------------------------------------------------------------
private var _data:object;
private var _callback:function;
public function appevent(type:string, data:object = null, callback:function = null)
{
super(type);
_data = data;
_callback = callback;
}
/**
* the data will be passed via the event. it allows the event dispatcher to publish
* data to event listener(s).
*/
public function get data():object
{
return _data;
}
/**
* @private
*/
public function set data(value:object):void
{
_data = value;
}
/**
* the callback function associated with this event.
*/
public function get callback():function
{
return _callback;
}
/**
* @private
*/
public function set callback(value:function):void
{
_callback = value;
}
/**
* override clone
*/
public override function clone():event
{
return new appevent(this.type, this.data, this.callback);
}
/**
* dispatch this event.
*/
public function dispatch():boolean
{
return eventbus.instance.dispatchevent(this);
}
/**
* dispatch an appevent for specified type and with optional data and callback reference.
*/
public static function dispatch(type:string, data:object = null, callback:function = null):boolean
{
return eventbus.instance.dispatchevent(new appevent(type, data, callback));
}
public static function addlistener(type:string, listener:function, usecapture:boolean = false, priority:int = 0, useweakreference:boolean = false):void
{
eventbus.instance.addeventlistener(type, listener, usecapture, priority, useweakreference);
}
public static function removelistener(type:string, listener:function, usecapture:boolean = false):void
{
eventbus.instance.removeeventlistener(type, listener, usecapture);
}
}
}
eventbus.as
package com
{
import flash.events.event;
import flash.events.eventdispatcher;
/**
* the eventbus allows centrallized communication among modules without
* point-to-point messaging. it uses the singleton design pattern
* to make sure one event bus is available globally. the bus itself
* is only available to the container. modules use the container's
* static method to communicate with the event bus.
*/
public class eventbus extends eventdispatcher
{
/** application event bus instance */
public static const instance:eventbus = new eventbus();
/**
* normally the eventbus is not instantiated via the <b>new</b> method directly.
* the constructor helps enforce only one evenbus availiable for the application
* (singeton) so that it asures the communication only via a sigle event bus.
*/
public function eventbus()
{
}
/**
* the factory method is used to create a instance of the eventbus. it returns
* the only instanace of eventbus and makes sure no another instance is created.
*/
[deprecated(replacement="instance")]
public static function getinstance():eventbus
{
return instance;
}
/**
* basic dispatch function, dispatches simple named events. in the case
* that the event is only significant by the event token (type string),
* this new dispatch method simplify the code.
*/
[deprecated(replacement="appevent.dispatch")]
public function dispatch(type:string):boolean
{
return dispatchevent(new event(type));
}
}
}
发表评论