samira
samira

Reputation: 117

stopPropagation doesn't work in flex

i have a root component A and two component B , C that C is front of B

B and C have EventListener

B.addEventLisener(MouseEvent.MouseDown,handler1);
C.addEventLisener(MouseEvent.MouseDown,handler2);

i want when c catch the event it doesn't bubble to B

i use

private function handler2(e:MouseEvent):void{
e.stopPropagation();
allowDraw = true;}

in handler2 but it doesn't work!

Upvotes: 0

Views: 113

Answers (1)

Timofei Davydik
Timofei Davydik

Reputation: 7294

Seems, that C and B are children of A. But if C is not child of B, it will not bubble from C to B. Bubbling works only from child to parent. But if B and C are both children of A and you added C after B, it doesn't mean that C became child of B.

That's why event handlers are executed in order they are added. You have 2 solutions:

1.swap adding handlers

C.addEventLisener(MouseEvent.MouseDown,handler2);
B.addEventLisener(MouseEvent.MouseDown,handler1);

2.use event priorities

B.addEventLisener(MouseEvent.MouseDown,handler1, false, 10);
C.addEventLisener(MouseEvent.MouseDown,handler2, false, 20);

Moreover, stopPropagation won't work as well. You'll need to remove listener.

UPD

If you don't want to remove listeners, use boolean flag;

private function handler2(e:MouseEvent):void {
    e.stopPropagation();
    allowDraw = true;
    stop = true;
}

private function handler1(e:MouseEvent):void {
    if (stop) {
        stop = false;
        return;
    }
    //handler code
}

Upvotes: 2

Related Questions