views:

123

answers:

2

Hi Guys,

I have a flex application, the display of this application is build with many containers. I have a FlexEvent.UPDATE_COMPLETE on each of the displayObjects.

What do I want to accomplish? I want to handle the event only on the top level where it occurred, for instance, if I have a grid and the update occurred in a label somewhere inside, I want to handle the event only on the Grid.

is there a way to accomplish that?

just to emphasize, I don't have any knowledge of the display objects at compile time, only at runtime, the display is being built dynamically to I can't just write the code on the grid, I have to check somehow if the event occurred in a higher level.

I would love some help with this issue, even if it's not code but concept of how to handle this unique issue.

Thanks

A: 

Have you considered stopPropagation()/stopImmediatePropagation() on the event, once you handle that event.

Example: Since your button is in canvas. Your event handler method in canvas would look like this,

function handleEvent(e:FlexEvent):void {
 trace("In Canvas's handler");
 //do your events...
 e.stopPropagation(); //This stops from propagating e to its parent containers, which is an HBOX. The container can be anything at runtime, it doesnt affect the propagation.

}

Try the same example in your other containers too.

Some examples here,

http://livedocs.adobe.com/flex/3/html/help.html?content=events_08.html

http://livedocs.adobe.com/flex/3/langref/flash/events/Event.html#stopPropagation%28%29

prem
I'm ware of these, how can they help me here?I'll give an example, I have a button, it's contained within a canvas and this canvas is in an HBOX (again, I don't have this knowledge in compile time, only in runtime)once the event is fired, it will fire on the button and on the Canvas but not on the HBox, I want to handle this event only once, on the Canvas.
Avi Tzurel
Edited my answer, the problem is just to stop the event propagation.
prem
the parents might be relying on child's updateComplete event - this might break something... gotta check Flex component instantiation life cycle to confirm.
Amarghosh
A: 

Just check for the event.target and ignore if it is not what you're looking for. Or even better: listen for events on top level components and ignore if target and currentTarget doesn't match.

if(event.target != event.currentTarget) 
    return;

If you can't do this either, check the parents: if parent is your application or the container that holds the top level items, it is a top level item. Based on the structure of your component, it can be anything like

if(event.target.parent == this)
//or
if(event.target.parent == this.theContainer_thatHolds_topLevelItems)
//or 
if(event.target.parent is Application)
//or 
if(event.target.parent is CustomContainerClassName)
Amarghosh