Flex 4.5 不监听自定义 AS 类中调度的自定义事件

发布于 2024-12-13 14:37:41 字数 3389 浏览 0 评论 0原文

我遇到以下情况:

我有一个事件处理程序,它在应用程序的状态栏中显示小消息。 这些消息通过从自定义组件分派事件来传递。 一条简单的消息可能类似于“HTTP 错误”等。

现在,主应用程序文件中的主事件侦听器侦听任何自定义组件分派的事件,但似乎拒绝侦听自定义 AS 类分派的事件。

这是我的自定义事件代码:

package main.events
{
    import flash.events.Event;

    public class ShowNoticeEvent extends Event
    {
        public var message:String;
        public static const SHOW_NOTICE:String = "showNotice";

        public function ShowNoticeEvent(type:String, msg:String, bubbles:Boolean = false, cancelable:Boolean = false)
        {
            super(type, bubbles, cancelable);
            this.message = msg;
        }

        override public function clone():Event
        {
            return new ShowNoticeEvent(type, message);
        }
    }
}

这是主应用程序文件中的事件侦听器:

addEventListener(ShowNoticeEvent.SHOW_NOTICE, showNoticeListener, true);

这是调度自定义事件的自定义 AS 类。我粘贴了所有代码,这样您就可以看到它的整个部分。

   package components.dashboard
{
    import components.dashboard.models.*;

    /* Event imports */
    import flash.events.*;
    import main.events.*;

    import mx.controls.Alert;
    import mx.core.UIComponent;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.http.HTTPService;

    [Event(name="showNotice", type="main.events.ShowNoticeEvent")]
    public class Controller extends UIComponent
    {
        private var baseUrl:String;

        public function Controller(baseUrl:String)
        {
            this.baseUrl = baseUrl;
        }

        public function getRunningQuotations():void
        {
            var runningQuotationsList:RunningQuotationsList = RunningQuotationsList.getInstance();

            execService("index.php?a=1", runningQuotationsList.updateList, "pnlRunningQuotations"); 
        }

        public function getRecentProjects():void
        {
            var recentProjectsList:RecentProjectsList = RecentProjectsList.getInstance();

            execService("index.php?a=2", recentProjectsList.updateList, "pnlRecentProjects");
        }

        public function getLatestCustomers():void
        {
            var latestCustomersList:LatestCustomersList = LatestCustomersList.getInstance();

            execService("index.php?a=3", latestCustomersList.updateList, "pnlLatestCustomers");
        }

        private function execService(url:String, listener:Function, component:String):void
        {
            var basicService:HTTPService = new HTTPService(baseUrl);
            basicService.showBusyCursor = true;
            basicService.addEventListener(FaultEvent.FAULT, function(e:FaultEvent):void{httpFault(e, component)});
            basicService.method = "POST";
            basicService.resultFormat = "text";
            basicService.url = url;
            basicService.addEventListener(ResultEvent.RESULT, listener);
            basicService.send();            
        }

        private function httpFault(event:FaultEvent, component:String = null):void {
            var faultstring:String = event.fault.faultString;
            var eventObj:ShowNoticeEvent = new ShowNoticeEvent(ShowNoticeEvent.SHOW_NOTICE, faultstring, true); 

            dispatchEvent(eventObj);
            trace(faultstring);
        }
    }
}

总结一下: - 事件侦听器侦听任何自定义组件调度的自定义事件。 - 事件侦听器不侦听 AS 类发送的自定义事件。

那些想知道的人,事件真的被调度了,这就是我添加跟踪调用的原因。

I have the following situation:

I have an event handler, that displays small messages in my application's statusbar.
These messages get passes through by dispatching events from custom components.
A simple message could be like "HTTP Error" or so.

Now, the main event listener, in the main application file, listens to the event dispatched by any custom component, but seems to refuse listening to events dispatched by custom AS classes.

Here is my code for the custom event:

package main.events
{
    import flash.events.Event;

    public class ShowNoticeEvent extends Event
    {
        public var message:String;
        public static const SHOW_NOTICE:String = "showNotice";

        public function ShowNoticeEvent(type:String, msg:String, bubbles:Boolean = false, cancelable:Boolean = false)
        {
            super(type, bubbles, cancelable);
            this.message = msg;
        }

        override public function clone():Event
        {
            return new ShowNoticeEvent(type, message);
        }
    }
}

This is the event listener in the main application file:

addEventListener(ShowNoticeEvent.SHOW_NOTICE, showNoticeListener, true);

And this is the custom AS class that dispatches the custom event. I pasted all the code, so you could see the whole part of it.

   package components.dashboard
{
    import components.dashboard.models.*;

    /* Event imports */
    import flash.events.*;
    import main.events.*;

    import mx.controls.Alert;
    import mx.core.UIComponent;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.http.HTTPService;

    [Event(name="showNotice", type="main.events.ShowNoticeEvent")]
    public class Controller extends UIComponent
    {
        private var baseUrl:String;

        public function Controller(baseUrl:String)
        {
            this.baseUrl = baseUrl;
        }

        public function getRunningQuotations():void
        {
            var runningQuotationsList:RunningQuotationsList = RunningQuotationsList.getInstance();

            execService("index.php?a=1", runningQuotationsList.updateList, "pnlRunningQuotations"); 
        }

        public function getRecentProjects():void
        {
            var recentProjectsList:RecentProjectsList = RecentProjectsList.getInstance();

            execService("index.php?a=2", recentProjectsList.updateList, "pnlRecentProjects");
        }

        public function getLatestCustomers():void
        {
            var latestCustomersList:LatestCustomersList = LatestCustomersList.getInstance();

            execService("index.php?a=3", latestCustomersList.updateList, "pnlLatestCustomers");
        }

        private function execService(url:String, listener:Function, component:String):void
        {
            var basicService:HTTPService = new HTTPService(baseUrl);
            basicService.showBusyCursor = true;
            basicService.addEventListener(FaultEvent.FAULT, function(e:FaultEvent):void{httpFault(e, component)});
            basicService.method = "POST";
            basicService.resultFormat = "text";
            basicService.url = url;
            basicService.addEventListener(ResultEvent.RESULT, listener);
            basicService.send();            
        }

        private function httpFault(event:FaultEvent, component:String = null):void {
            var faultstring:String = event.fault.faultString;
            var eventObj:ShowNoticeEvent = new ShowNoticeEvent(ShowNoticeEvent.SHOW_NOTICE, faultstring, true); 

            dispatchEvent(eventObj);
            trace(faultstring);
        }
    }
}

So to sum it all up:
- The event listener listens to the custom event dispatched by any custom component.
- The event listener does not listen to the custom event duspatched by an AS class.

Those who wonder, the event really gets dispatched, that's why I added a trace call.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

忆悲凉 2024-12-20 14:37:41

必须将控制器类的实例添加到舞台中才能使其正常工作。

通过

addEventListener(ShowNoticeEvent.SHOW_NOTICE, showNoticeListener, true);

在主文件中执行操作,您可以将侦听器添加到舞台中。

所以基本上您正在执行操作。

stage.addEventListener(ShowNoticeEvent.SHOW_NOTICE, showNoticeListener, true);

如果控制器实例不在舞台上,您将看不到该事件。

您可能想要研究用于数据管理的单例类型模式,因为它非常适合此设置。

The instance of Controller Class would have to be added to stage for that to work.

by doing

addEventListener(ShowNoticeEvent.SHOW_NOTICE, showNoticeListener, true);

in the main file you are adding the listener to the stage.

So basically you are doing.

stage.addEventListener(ShowNoticeEvent.SHOW_NOTICE, showNoticeListener, true);

If controler instance is not on stage you won't see the event.

You might want to look into a Singleton type pattern for your data management as that would fit this setup pretty good.

撩动你心 2024-12-20 14:37:41

主要的:

Controller.getLastInstance().addEventListener(ShowNoticeEvent.SHOW_NOTICE, showNoticeListener, true)

package components.dashboard
{
    import components.dashboard.models.*;

    /* Event imports */
    import flash.events.*;
    import main.events.*;

    import mx.controls.Alert;
    import mx.core.UIComponent;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.http.HTTPService;

    [Event(name="showNotice", type="main.events.ShowNoticeEvent")]
    public class Controller extends UIComponent
    {
        static public function getLastInstance():Controller { return _instance; }
        static private var _instance:Controller;
        private var baseUrl:String;

        public function Controller(baseUrl:String)
        {
            _instance = this;
            this.baseUrl = baseUrl;
        }

        public function getRunningQuotations():void
        {
            var runningQuotationsList:RunningQuotationsList = RunningQuotationsList.getInstance();

            execService("index.php?a=1", runningQuotationsList.updateList, "pnlRunningQuotations"); 
        }

        public function getRecentProjects():void
        {
            var recentProjectsList:RecentProjectsList = RecentProjectsList.getInstance();

            execService("index.php?a=2", recentProjectsList.updateList, "pnlRecentProjects");
        }

        public function getLatestCustomers():void
        {
            var latestCustomersList:LatestCustomersList = LatestCustomersList.getInstance();

            execService("index.php?a=3", latestCustomersList.updateList, "pnlLatestCustomers");
        }

        private function execService(url:String, listener:Function, component:String):void
        {
            var basicService:HTTPService = new HTTPService(baseUrl);
            basicService.showBusyCursor = true;
            basicService.addEventListener(FaultEvent.FAULT, function(e:FaultEvent):void{httpFault(e, component)});
            basicService.method = "POST";
            basicService.resultFormat = "text";
            basicService.url = url;
            basicService.addEventListener(ResultEvent.RESULT, listener);
            basicService.send();            
        }

        private function httpFault(event:FaultEvent, component:String = null):void {
            var faultstring:String = event.fault.faultString;
            var eventObj:ShowNoticeEvent = new ShowNoticeEvent(ShowNoticeEvent.SHOW_NOTICE, faultstring, true); 

            dispatchEvent(eventObj);
            trace(faultstring);
        }
    }
}

不理想,因为您只能拥有其中 1 个。
但我认为,这比将简单的 EventDispatcher 转换为 DisplayObject 并将其添加到舞台上只是为了简单地冒泡更好。

Main:

Controller.getLastInstance().addEventListener(ShowNoticeEvent.SHOW_NOTICE, showNoticeListener, true)

.

package components.dashboard
{
    import components.dashboard.models.*;

    /* Event imports */
    import flash.events.*;
    import main.events.*;

    import mx.controls.Alert;
    import mx.core.UIComponent;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.http.HTTPService;

    [Event(name="showNotice", type="main.events.ShowNoticeEvent")]
    public class Controller extends UIComponent
    {
        static public function getLastInstance():Controller { return _instance; }
        static private var _instance:Controller;
        private var baseUrl:String;

        public function Controller(baseUrl:String)
        {
            _instance = this;
            this.baseUrl = baseUrl;
        }

        public function getRunningQuotations():void
        {
            var runningQuotationsList:RunningQuotationsList = RunningQuotationsList.getInstance();

            execService("index.php?a=1", runningQuotationsList.updateList, "pnlRunningQuotations"); 
        }

        public function getRecentProjects():void
        {
            var recentProjectsList:RecentProjectsList = RecentProjectsList.getInstance();

            execService("index.php?a=2", recentProjectsList.updateList, "pnlRecentProjects");
        }

        public function getLatestCustomers():void
        {
            var latestCustomersList:LatestCustomersList = LatestCustomersList.getInstance();

            execService("index.php?a=3", latestCustomersList.updateList, "pnlLatestCustomers");
        }

        private function execService(url:String, listener:Function, component:String):void
        {
            var basicService:HTTPService = new HTTPService(baseUrl);
            basicService.showBusyCursor = true;
            basicService.addEventListener(FaultEvent.FAULT, function(e:FaultEvent):void{httpFault(e, component)});
            basicService.method = "POST";
            basicService.resultFormat = "text";
            basicService.url = url;
            basicService.addEventListener(ResultEvent.RESULT, listener);
            basicService.send();            
        }

        private function httpFault(event:FaultEvent, component:String = null):void {
            var faultstring:String = event.fault.faultString;
            var eventObj:ShowNoticeEvent = new ShowNoticeEvent(ShowNoticeEvent.SHOW_NOTICE, faultstring, true); 

            dispatchEvent(eventObj);
            trace(faultstring);
        }
    }
}

Not Ideal since you could only ever have 1 of them.
But I think better than having to turn a simple EventDispatcher into DisplayObject and add it to stage just to Simply bubble.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文