:::: MENU ::::
Posts tagged with: list

Listening for events in a (Flex) Spark Itemrenderer

I recently ran into an issue where I was creating a custom itemrender in Flashbuilder (Flex) which contained a button. I wanted to listen for the button’s Click event and have an event handler within the itemrenderer respond to it. Sounds simple enough; however it did not work.. AT ALL! After a bit of fumbling around, and a whooooole lot of Googling, I found the problem AND (more importantly) the solution!

What was happening was this: When the button inside of my itemrenderer was clicked, the itemrenderer itself was receiving the click event first, and after notifying the parent list that it had been clicked, some updating took place in the list and all further events were killed (including my button’s click event). I’m sure there are way more technical ways to explain exactly what was occurring; however, I’m lazy, and I have a rather simple fix which works just fine for my purposes, so I’ll spare you the tech jargon and smart talk for now..

THE FIX:
Instead of listening for the buttons MouseEvent.CLICK event, I instead listen for MouseEvent.MouseDown. That’s it (told you it was simple). Apparently, the MouseDown event is triggered before the click event (which actually makes quite a bit of sense), and as such, you are able to capture it before the itemrenderer swoops in and steals all the glory (and the events)!

THE GOTCHA:
It’s important to note, in the MouseDown event handler inside of your itemrenderer, you MUST call event.stopImmediatePropogation() on the event that gets passed into the handler method. Failing to do so will allow the event to bubble up the chain and once again give the itemrenderer a chance to steal the show. Calling event.stopImmediatePropogation() will stop the event dead in its’ tracks, and give you a chance to apply any logic you might have in your event handler (I’m assuming there is actually a reason you want to capture the event, other than making it your bitch..).

EXAMPLE ITEMRENDERER: