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?
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.
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.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With