GWT Event Preview vs Event Handler

会有一股神秘感。 提交于 2020-01-02 06:22:08

问题


My question is, what's the different between event preview and event handler in GWT.

There is a callback function boolean onEventPreview(Event event) for event preview and a callback function void onBrowserEvent(Event event) as well. They are pretty similar, so what's the different between them? Especially when should I use the event preview at all when the event handler works perfect?

thanks


回答1:


DOM.addEventPreview(EventPreview preview) lets you place an event preview on top of the event stack, which is called before any onBrowserEvent(Event event) is fired. This way you can place some logic before the event firing takes place. You can even prevent the event from firing by returning false. For example below example prevents the browser from reacting to mousemove and mousedown events.(Click and drag an image, browser won't drag an outline of image)

    DOM.addEventPreview(new EventPreview() {
        @Override
        public boolean onEventPreview(Event event) {
            switch (DOM.eventGetType(event)){
                case Event.ONMOUSEDOWN:
                case Event.ONMOUSEMOVE:
                    event.preventDefault();
            } 
            return true;
        }
    });

Just a reminder, adding eventPreviews this way is depreciated. Correct way to do it is to use Event.addNativePreviewHandler(NativePreviewHandler handler)




回答2:


From the javadoc:

As long as this preview remains on the top of the stack, it will receive all events before they are fired to their listeners. Note that the event preview will receive all events, including those received due to bubbling, whereas normal event handlers only receive explicitly sunk events.

You can return false from onEventPreview to cancel the event, in which case the event handlers will not be fired.



来源:https://stackoverflow.com/questions/7076004/gwt-event-preview-vs-event-handler

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!