1. #1
    Sencha User
    Join Date
    Nov 2013
    Location
    Bangalore, India
    Posts
    98
    Vote Rating
    7
    vardhan22 is on a distinguished road

      0  

    Default Can we attach any event handler to any widget

    Can we attach any event handler to any widget


    Hi All,
    in GXT 3, in every widget, we have a method
    Code:
    com.google.gwt.user.client.ui.Widget.addHandler(EventHandler handler, Type<EventHandler> type)
    
    Can we use this to attach any handler to any widget. There are several events mention in
    Code:
     com.google.gwt.event.dom.client
    as well as in
    Code:
    com.sencha.gxt.core.client.event
    .

    For ex. Can we add a double click handler using this to a textField? Will it listen to double click event. ValueBaseField implements implements HasKeyPressHandlers, HasKeyDownHandlers, HasChangeHandlers, HasParseErrorHandlers. So, will it listen to only those event which are associated with these handlers?

    Can we attach some of those events (not all) , which are not associated with the handlers? Is it possible? Without extending the class to implement the handlers

    Thanks

  2. #2
    Sencha - GXT Dev Team
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,732
    Vote Rating
    90
    Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light

      1  

    Default


    In short, yes, that will add an event handler, so that anything which calls widget.fireEvent will trigger any associated handlers that have been added with addHandler.

    That said, if nothing actually fires those events, addHandler is pretty much useless, so you need to make sure that the widget is capable of firing those events.

    In general, there are two sorts of events that get fired from widgets, the 'physical' events, the details that the browser sends directly, and the 'logical' events, the parts of the widget that make it useful to interact with. Usually if you are listening to a logical event (afaik all events in the com.sencha.gxt package are logical, and any event without 'dom' in its package in com.google.gwt is probably logical as well), there is almost always an addFooHandler, where Foo is the type of event, so using addHandler is a bit silly.

    On the other hand, when listening to any dom event, the widget may not account for it. We try not to expose dom events directly in GXT, but many GWT widgets do. If you need access to a dom event, use addDomHandler in place of addHandler - this not only adds a handler for the event you are interested in, but also wires up the element to properly send that event out if it happens.

    So why do we even have addHandler if it usually doesnt make sense to use it for either type of event? Take a look inside any other add*Handler method - it probably uses addHandler under the hood to actually do *its* job. If you build your own widgets to solve your own specific use cases, you can do this too.

  3. #3
    Sencha User
    Join Date
    Nov 2013
    Location
    Bangalore, India
    Posts
    98
    Vote Rating
    7
    vardhan22 is on a distinguished road

      0  

    Default


    Quote Originally Posted by Colin Alworth View Post
    If you need access to a dom event, use addDomHandler in place of addHandler - this not only adds a handler for the event you are interested in, but also wires up the element to properly send that event out if it happens.
    Using addDomHandler in textField for adding a handler double click event does not work. You said GXT widget may not listen to DOM events, so instead of addHnadler, we should use addDomHandler

  4. #4
    Sencha - GXT Dev Team
    Join Date
    Feb 2009
    Location
    Minnesota
    Posts
    2,732
    Vote Rating
    90
    Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light Colin Alworth is a glorious beacon of light

      0  

    Default


    Without more details I can't tell for sure, but there are a number of cases that could cause what you are describing. The main one is that the element isn't sending a doubleclick event at all, so there is no event to forward to your handler.

    This could be happening because a preview handler is preventing the event, either by stopping either of the two click events, or stopping the doubleclick itself. There are other possible ways that this could be stopped as well, I would need a sample that doesn't work to give you more details.

    This does work, however:
    Code:
        TextField firstName = new TextField();
        firstName.addDomHandler(new DoubleClickHandler() {
          @Override
          public void onDoubleClick(DoubleClickEvent event) {
            Window.alert("dblclick");
          }
        }, DoubleClickEvent.getType());
        RootPanel.get().add(firstName);
    By itself in an entrypoint, when you doubleclick on the field, the alert popup appears.

Thread Participants: 1

Tags for this Thread