Firstly, Happy New Year! Hope your ‘09 is off to a flyer!
Secondly, sorry about the rather mundane heading - couldn’t really think of what to call this post. I’ve been typing all sorts of things into Google trying to find a solution to what I’ll describe below.
I’m continuing the project I started in early December and having some very interesting battles with AS3 and the Flex framework. I’d like to document one of those battles today.
What I wanted to do was send parameters along with an event so that the event listener had access to them. Now I’m fully aware of how to do that when I’m in charge of dispatching the event, but was (and still am) at a loss as to how to add them in the following instance:
myComboBox.addEventListener(Event.CHANGE, eventListener);
This call to addEventListener occurs where the variables I want to pass get defined.
Here’s the code that I attempted to use (without luck) to get the parameters passed along with the event:
myComboBox.addEventListener(PodEvent.CHANGE, eventListener); var myEvent:PodEvent = new PodEvent(PodEvent.CHANGE); myEvent.displayTriggerValue = displayTriggerValue; myEvent.formItem = triggerDisplay; myComboBox.dispatchEvent(myEvent);
I could get the displayTriggerListener to run when the combobox changed, but the variables that I added to myEvent wouldn’t make it in there (further to that I was having type coercion difficulties, but that’s another story I think). The initial dispatchEvent was also allowing the eventListener to get the parameters, but obviously the idea is to have them passed along with every PodEvent.CHANGE event.
Now initially I got around this using closures (a new term to me). I understand the issues of using anonymous functions (ie. garbage collection issues) so searched some more for answers. I found nada.
So after a nights rest and a few more hours wrestling with it today, I’ve come up with the following solution that I’m rather chuffed about. I use a Dictionary to store a reference to the component and in turn create an object associated with that Dictionary element to store the parameters I want to pass into the eventListener function. Perhaps some code will illustrate what the hell I just typed (for me and you both ;-)
myParamDictionary[myComboBox] = {param1: value1, param2: value2};
The myParamDictionary is a private var in the class so is accessible within the eventListener
private function eventListener(e:Event):void {
var _local:* = myParamDictionary[e.target]
// rest of the implementation here. You can access the previously set parameters by _local.param1, _local.param2...
}
And to my utter delight (and surprise) it works!
The only downside is there’s no type checking on the parameters, which is what I originally wanted to use a custom event for.
If you’ve got a suggestion for my initial problem or a critique for my end solution please don’t be shy and post them!
Recent Comments