Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are EventHandlers and SinkEvents doing the same functional job?

Tags:

gwt

I am using GWT. I started adding events to my widgets by adding EventHandlers.

Event handler sample code:

    widget.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            // do something
        }
    });

I then found there is another way to handle events using sinkEvents().

Sink events sample code (from this website):

 {
 ...
 sinkEvents(Event.ONMOUSEDOWN | Event.ONMOUSEUP | Event.ONMOUSEOVER |Event.ONMOUSEOUT)
 ...
 }

 public void onBrowserEvent(Event event) {
     Element td = getEventTargetCell(event);
     if (td == null) return;
     Element tr = DOM.getParent(td);
     switch (DOM.eventGetType(event)) {
         case Event.ONMOUSEDOWN: {
                 // do something
                 break;
         }
         case Event.ONMOUSEUP: {
                 // do something
                 break;
         }
         case Event.ONMOUSEOVER: {
                 // do something
                 break;
         }
         case Event.ONMOUSEOUT: {
                 // do something
                 break;
         }
     }
 }
  1. Are EventHandlers and SinkEvents doing the same functional job?
  2. If yes, what are the tradeoffs? (where would you use one over the other)
  3. If no, how are they different?
like image 859
Trevor Boyd Smith Avatar asked May 20 '11 19:05

Trevor Boyd Smith


People also ask

What is the difference between event handlers and event listeners?

The minor difference between event handlers and event listeners is easy to understand: You can only have one event handler for a specific event type, but you can add multiple event listeners for it. Let’s see what happens if you try to add two onclick event handlers to the same object:

How does event handling work?

This way, anytime a new meetup is scheduled, you get alerted. That is event handling! The "event" here is a new JS meetup. When a new meetup is posted, the website meetup.com catches this change, thereby "handling" this event.

Is the event handler in the bubbling or capture phase?

If useCapture is set to false, the event handler is in the bubbling phase. Otherwise it's in the capture phase. The order of the phases of the event depends on the browser.

How to handle events in Salesforce?

is output into the console. The other option to handling events is by using event listeners. An event listener is something you assign to an object. As the name suggests, the event listener listens for events and gets triggered when an event occurs. Let’s repeat the previous example by assigning an event listener to a button to listen for clicks.


1 Answers

I'm not GWT expert, but this is what I gather from looking at the GWT source:

  1. All EventHandlers (eventually) call addDomHandler(..) which calls sinkEvents(). sinkEvents() is lower-level and is a browser-abstraction wrapper around native javascript event handling.

    EventHandlers are build on top of sinkEvents so they provide all functionality that sinkEvents does.

    But usage-wise they are different: with EventHandlers you can register for different event types with different event handlers that can reside in different classes. Events will be automatically routed to appropriate handlers. With sinkEvents you can register for different event types (via an int, not type-safe), but always this widget's onBrowserEvent(event) will be called.

  2. EventHandlers add certain overhead. It's debatable if this matters at all.

  3. EventHandlers are a type-safe way to add and remove event handlers and an automatic way for events to be routed to the registered handlers of your choice. If you use GWT Widgets than you should use EventHandlers.

like image 60
Peter Knego Avatar answered Jan 19 '23 10:01

Peter Knego