tags:

views:

160

answers:

3

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

+1  A: 
private function doRememberButton(ev: Event) {

//this gives your button
ev.currentTarget;


}
Rick J
+2  A: 

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

macke
or doRememberButton(event.target) doesn't require you to change your existing code.
toby
+1  A: 

here is a solution more precisely the way u needed

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

at the function:

private function doRememberButton(thisBtn:Button):void
{
    ...
}

that's it! :)

Sris