Flex: 自定义事件 [Custome Event] 使用规范

Categories: 垃圾山 February 20th, 2009 20:18

Flash.events.EventDispacher类提供了事件dispatch函数,如果需要dispatch事件, 则应该考虑继承该类或其子类.如果不能继承该类,则应事先IEventDispatcher接口

自定义事件:

package com.insprise.dept.event
{

/**
 * when Operation Success, and it's responder can dispatch this event
 */
public class OperationResultEvent extends Event
{
	public static const OPERATION_COMPLETE:String = "OperationComplete";
	private var _resultData:Object;

	/**
	 * Constructor
	 */
	public function OperationResultEvent(resultData_:Object) {
		super(OPERATION_COMPLETE, false, false);
		_resultData = resultData_;
	}

	/**
	 * Getter
	 */
	 public function get resultData():Object {
	 	return _resultData;
	 }

	/**
	 * Override - MUST - 必须重写, 否则在clone时将不存在自定义的field
	 */
	 override public function clone():Event {
	 	return new OperationResultEvent(_resultData);
	 }
	
	/**
	 * OPTIONAL - 可选
	 */ 
	 override public function toString():String {
	 	return formatToString(OPERATION_COMPLETE, "_resultData");
	 }

}//end of class
}//end of package

如在某实现了IResponder接口的类中需要派发该事件:

package com.insprise.dept.view
{

/**
 * Select Departments Responder
 */
 [Event(name = "OperationComplete", type="com.insprise.dept.event.OperationResultEvent")]
public class OperationResponder  extends EventDispatcher implements IResponder
{
	public function OperationResponder() {
		super();
	}

	public function result(data:Object):void {
//do something
		dispatchEvent(new OperationResultEvent(data)); //Create and dispatch the Event
	}

	public function fault(info:Object):void {
		throw new Error("服务器出错 - 调用服务器端方法失败" +  ErrorMessage);
	}

}//end of class
}//end of package

<->



// Proudly powered by Apache, PHP, MySQL, WordPress, Bootstrap, etc,.