Xml – In Flex, is there something like a ‘this’ reference for an MXML component

apache-flexmxml

I could code what I want to achieve like this:

<mx:Button id="someButton" click="doRememberButton(someButton)" ... />

but would find it very helpful (I am putting together a rather large UI) if I could write:

<mx:Button click="doRememberButton(this)" ... />

Now, the obvious problem is that 'this' does not point to the Button, but to the main component defined by the file the code is in (e.g. VBox), yet it would be a great help if I had some reference to the 'current' MXML component..

Would anybody have a solution for this? Thanks!
Tom

Best Answer

Inline event handlers is really just wrapped code, so you may use the event object to get details of the dispatcher and other event information. Like so:

<mx:Button click="trace(event.target)" />

In your case, you'd have to change the signature of your event handler, e.g.:

private function doRememberButton(event:Event):void
{
    ...
}

And in the MXML code:

<mx:Button click="doRememberButton(event)" />

The target property of the event class is the original dispatcher of the event. There is also a currentTarget property which is the current target in the event chain. This relates to event bubbling. There is more information on this in Adobe LiveDocs

Related Topic