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?
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];
}
}
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/
精彩评论