如果在flex开发中想用event来触发事件,相关component又离得很远,除了bind,还可以用全局event来触发。
1. 先定义一个event
package components.event
{
import flash.events.Event;
import flash.events.EventDispatcher;
public class LogoutEvent extends Event
{
public static const LOG_OUT:String = "LogoutEvent";
public static var dispatcher:EventDispatcher = new EventDispatcher();
public function LogoutEvent()
{
super(LOG_OUT);
}
}
}
2. 在需要监听的component中添加监听。
LogoutEvent.dispatcher.addEventListener(LogoutEvent.LOG_OUT, onLogout);
private function logout():void
{
var obj:Object = GlobalParams.initParams("user", "logout");
logoutService.url = "/logout";
logoutService.send(obj);
}
3. 发送event:
LogoutEvent.dispatcher.dispatchEvent(new LogoutEvent());
4. 如果改组件需要被关闭或者删除,一定记得在remove的时候,把这个listener remove掉。。否则可能会引起内存泄漏。。
208

被折叠的 条评论
为什么被折叠?



