PDA

View Full Version : Stopping Event Propagation



thursday
16 Mar 2011, 12:47 AM
I have nested panels like ContentPanel B is added to ContentPanel A

Both the panels are registered with event listeners.

panelA.addListener(Events.OnClick, eventHandler);

panelB.addListener(Events.OnClick, eventHandler);

The eventHandler class contains the handleEvent method

public void handleEvent(BaseEvent domEvent) {
}

If I click Panel B, I get two events fired. One for Panel B and another one for Panel A

I want to stop the event propagating to Panel A as soon as I receive it Panel B

Please advise. I see the cancelBubble only for DomEvent

blay
16 Mar 2011, 12:52 AM
domEvent.setCancelled(true)

thursday
16 Mar 2011, 1:55 AM
domEvent.setCancelled(true)

It did not work. I still get events from the parent panel A.

From the docs,

True to cancel the current operation (defaults to false). Only applies to events that can be cancelled as defined by the object firing the event. Canceling an operation does not stop all listeners from being notified of the event.

thursday
16 Mar 2011, 7:34 AM
Darrel / sven,

Could you please kindly look into the issue?.

I need to understand how to stop the events going to parent panels using BaseEvent.

This is very critical for our project.

sven
16 Mar 2011, 9:54 AM
You can simple replace BaseEvent by DomEvent. ContentPanel is a Container and so fires a ContainerEvent which is a subclass of DomEvent.

thursday
17 Mar 2011, 3:54 AM
You can simple replace BaseEvent by DomEvent. ContentPanel is a Container and so fires a ContainerEvent which is a subclass of DomEvent.

Thanks Sven for the reply. It works now with cancelBubble. I did try with the handleEvent(DomEvent domEvent) earlier. But it failed for SelectionChangedEvent with the ClassCastException. (I have global event handler for all kinds of events at central place)

<code>
java.lang.ClassCastException: com.extjs.gxt.ui.client.event.SelectionChangedEvent cannot be cast to com.extjs.gxt.ui.client.event.DomEvent
</code>

I have changed my combo box event type to Event.Select from Event.SelectionChange now. I do not see the ClassCastException anymore.

Thanks again for your help. I am marking this as RESOLVED now.

sven
17 Mar 2011, 10:32 AM
SelectionChangedEvent will not be fired in the sample code you gave us here

thursday
17 Mar 2011, 9:04 PM
Sven,

The problem was resolved with your answer. I was trying to explain why I was using handleEvent(BaseEvent baseEvent) instead of handleEvent(DomEvent domEvent).

I have one common event handler which registers for events depending upon of the widget type

button.addListener(Events.OnClick, eventHandler);
combo.addListener(Events.Select, eventHandler);

All of my widgets are created at one place using a factory. For combo box, I used Event.SelectionChanged type which resulted in the ClassCastException.

Now I have changed it to Events.Select and it works fine with your suggestion.

Thanks again!.

sven
17 Mar 2011, 11:04 PM
If you use multiply event types for the same listener than you need to use BaseEvent, but you can do an instanceof check against DomEvent in the listener