Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can I set up multiple listeners for one event?

I want to set up multiple listeners for one event, and have found that using composite listener is the key.

Could anyone give me an example?

like image 653
Vicky Avatar asked Mar 28 '11 21:03

Vicky


People also ask

How do you attach multiple listeners to the same event?

The addEventListener() method You can add many event handlers of the same type to one element, i.e two "click" events. You can add event listeners to any DOM object not only HTML elements. i.e the window object. The addEventListener() method makes it easier to control how the event reacts to bubbling.

Can you have multiple event listeners for the same event?

We can add multiple event listeners for different events on the same element. One will not replace or overwrite another. In the example above we add two extra events to the 'button' element, mouseover and mouseout.

How many listeners can be attached to an event?

The maximum number of event listeners that can be attached to the event can be set by the setMaxListeners function and the default value is 10.

Can event listeners be nested?

The final step, the callback function, can be written as a nested anonymous function inside the event listener or can be a designated function fully defined in a separate function. The callback handles the resulting work you want to happen after an event has occurred.


1 Answers

class CompositeListener implements OnEventListener {    private List<OnEventListener> registeredListeners = new ArrayList<OnEventListener>();     public void registerListener (OnEventListener listener) {       registeredListeners.add(listener);    }     public void onEvent(Event e) {       for(OnEventListener listener:registeredListeners) {          listener.onEvent(e);       }    } } 

.....

CompositeListener composite = new CompositeListener(); composite.registerListener(listener1); composite.registerListener(listener2); component.setOnEventListener(composite); 
like image 170
Flavio Avatar answered Sep 20 '22 08:09

Flavio