处理来自单个侦听器的多个事件

发布于 2024-11-30 03:30:46 字数 1102 浏览 1 评论 0 原文

我想要一个针对多个事件的事件侦听器,具体取决于
我想单独处理它们的事件类型。

类似于 Swiz 框架 请参阅:“处理多个事件”来自单一方法'

我有一段代码,如

     var toolOptions:UIComponent=ToolOptions.createToolOptions(type);
            if (options != null)
            {
                options.addEventListener(Event.SELECT,toolOptionSelectedHandler);
                someViewComponent.addOptions(toolOptions);
            }

   // handle depending on event type
    private function toolOptionSelectedHandler(event:*):void
    {
        //handle depending on type of event fired
        // type cast  event depending on type and  retrieve VO from event 
        //and send handle it..

        //SomeToolObj.handle(event.VO);    

    }

上面的 toolOptions 是一个 mxml 组件,它是基于动态创建的上
'类型'。

另外,应该从组件中调度哪种类型的事件?例如:Event.SELECT

更准确地说,上面的内容基本上是工具栏所必需的。
当用户选择一个工具时,他会看到该工具的选项,当他选择选项时,
工具应该将它们应用到视图上的对象。

有更好的方法来做同样的事情吗?

I want to have a single event listener for multiple events and depending on the
type of event i want to handle them separately.

Something similar to Swiz framework see: 'Handling Multiple Events from a Single Method'

i have a piece of code like

     var toolOptions:UIComponent=ToolOptions.createToolOptions(type);
            if (options != null)
            {
                options.addEventListener(Event.SELECT,toolOptionSelectedHandler);
                someViewComponent.addOptions(toolOptions);
            }

   // handle depending on event type
    private function toolOptionSelectedHandler(event:*):void
    {
        //handle depending on type of event fired
        // type cast  event depending on type and  retrieve VO from event 
        //and send handle it..

        //SomeToolObj.handle(event.VO);    

    }

In above toolOptions is a mxml component which get dynamically created based on
'type'.

Also which type of event should be dispatch the event from the component? eg: Event.SELECT

To be more precise the above is basically required for a toolbar.
When user selects a tool,he is shown options for a tool and when he selects options,
tool should apply them to object on the view.

Is there a better way to do the same?

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

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

发布评论

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

评论(2

揽清风入怀 2024-12-07 03:30:46

这就是我的理解:

你有不同的工具。每个工具都有一个选项列表。您单击一个选项,单个事件处理程序应根据该选项执行某些操作。

object on the view 1 : n tools 1 : n options

使用属性 optionType 创建自定义事件 OptionEvent.SELECT

public class OptionEvent extends Event {
    public static const SELECT : String = "optionEvent_select";
    public var optionType : String;
    public function OptionEvent(type : String) {
        super(type, true); // bubbles
    }
}

当用户选择选项时,像这样调度事件:

var event : OptionEvent = new OptionEvent(OptionEvent.SELECT);
event.optionType = "border";
dispatchEvent(event);

像您一样监听事件:

var toolOptions:UIComponent=ToolOptions.createToolOptions(type);
if (options != null) {
    options.addEventListener(OptionEvent.SELECT,toolOptionSelectedHandler);
    someViewComponent.addOptions(toolOptions);
}

通过确定选项类型来区分选项类型:

private function toolOptionSelectedHandler(event : OptionEvent) : void {
     var optionType = event.optionType;
     switch (optionType) {
         case "border":
             addBorderToView();             
             break;
         case "rotation":
             rotateView();             
             break;
     }
}

更新 - 如何设置选项值列表在选项事件中:

public class OptionEvent extends Event {
    public static const SELECT : String = "optionEvent_select";
    public var optionType : String;
    private var _optionValues : Object;

    public function OptionEvent(type : String) {
        _optionValues = new Object();
        super(type, true); // bubbles
    }

    public function setOptionValue(property : String, value : *) : void {
        _optionValues[property] = value;
    }

    public function getOptionValue(property : String) : * {
        return _optionValues[property];
    }
}

That's what I understand:

You have different tools. Each of the tools has a list of options. You click an option, and a single event handler should perform some action depending on the option.

object on the view 1 : n tools 1 : n options

Create a custom event OptionEvent.SELECT with the property optionType.

public class OptionEvent extends Event {
    public static const SELECT : String = "optionEvent_select";
    public var optionType : String;
    public function OptionEvent(type : String) {
        super(type, true); // bubbles
    }
}

When the user selects the option, dispatch the event like this:

var event : OptionEvent = new OptionEvent(OptionEvent.SELECT);
event.optionType = "border";
dispatchEvent(event);

Listen to the event like you do:

var toolOptions:UIComponent=ToolOptions.createToolOptions(type);
if (options != null) {
    options.addEventListener(OptionEvent.SELECT,toolOptionSelectedHandler);
    someViewComponent.addOptions(toolOptions);
}

Distinguish the type of option by determining the option type:

private function toolOptionSelectedHandler(event : OptionEvent) : void {
     var optionType = event.optionType;
     switch (optionType) {
         case "border":
             addBorderToView();             
             break;
         case "rotation":
             rotateView();             
             break;
     }
}

UPDATE - How to set up a list of option values in OptionEvent:

public class OptionEvent extends Event {
    public static const SELECT : String = "optionEvent_select";
    public var optionType : String;
    private var _optionValues : Object;

    public function OptionEvent(type : String) {
        _optionValues = new Object();
        super(type, true); // bubbles
    }

    public function setOptionValue(property : String, value : *) : void {
        _optionValues[property] = value;
    }

    public function getOptionValue(property : String) : * {
        return _optionValues[property];
    }
}
初雪 2024-12-07 03:30:46

您可能还想尝试这样的事情:
http://blog.iconara.net/ 2008/03/30/separating-event-handling-from-event-filtering/

我已经通过使用带有链接的责任链完成了一个让我更满意的解决方案具有单独的 certainResponsibility 和 meetResponsibility 方法,其中在创建链接时存储特定事件数据。如果我们找到它与传递到链中的事件之间的匹配,那么我们调用 meetResponsibility。否则,我们调用链中的下一个链接。

有关 COR 的更多信息,请查看 http://www.as3dp.com/2008/01/14/actionscript-30-chain-of-responsibility-design-pattern-decoupling-request-and-request-handler/

You may also want to try something like this:
http://blog.iconara.net/2008/03/30/separating-event-handling-from-event-filtering/

and I've done a solution I'm a bit more comfortable with by using a Chain of Responsibility with links that have separate determineResponsibility and meetResponsibility methods, where the specific event data is stored when the link is created. If we find a match between that and the event that gets passed into the chain, then we call meetResponsibility. Otherwise, we call the next link in the chain.

For more on COR, check out http://www.as3dp.com/2008/01/14/actionscript-30-chain-of-responsibility-design-pattern-decoupling-request-and-request-handler/

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