GWT sinkEvent功能如何工作? [英] How does the GWT sinkEvent functionality works?

查看:107
本文介绍了GWT sinkEvent功能如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < g:HTMLPanel ui:field =containerwidth = 500pxheight =300px> 
< g:Label ui:field =innertext =hello>< / g:Label>
< / g:HTMLPanel>

添加处理程序:

  container.addDomHandler(new ClickHandler()
{
@Override
public void onClick(ClickEvent event)
{
Window.alert click on container);
}
},ClickEvent.getType());

inner.addDomHandler(new ClickHandler()
{
@Override
public void onClick(ClickEvent event)
{
Window.alert (click on inner);
}
},ClickEvent.getType());

我们从源头知道,每个这些addHandler调用


DOM.sinkEvents(Element elem,int eventBits)


里面,根据文档,后者根据文档设置当前给定元素的事件集,这些事件将被触发到任何元素的父节点上指定的最接近的{@link EventListener}。



但实际上,如果您点击内部div,点击次数将以三种方式中的任何一种方式被触发到其父div:




  • 如果我没有在孩子


  • 上设置任何ClickHandler, / p>


  • 如果我设置了处理程序,但是清除了ClickEvent的下沉与




DOM.sinkEvents(inner.getElement(),DOM.getEventsSunk(getElement())&〜Event.getTypeInt(ClickEvent.getType()。getName()));



为什么会发生这种情况,真正的作用是什么?沉没事件?
为什么添加处理程序与下沉事件有关,即将相同的事件触发给给定元素的父项?
是否可以防止所有处理程序,添加到给定的窗口小部件调用,而不必明确地删除它们?即有没有办法停止处理一些小部件上的所有事件?

解决方案

首先, sinkEvent 是关于在元素上添加本机事件处理程序,如 GWT维基



你看到的是事件冒泡,事件在浏览器中如何工作:调度事件时有两个阶段:捕获阶段(未在旧IE中实现)允许任何祖先元素在其达到目标之前捕获事件,然后在冒泡阶段,事件起泡DOM层次结构。因此,如果您收听 HTMLPanel 中的点击,您还将收到标签中的冒泡点击事件。 / p>

您可以通过监听标签级别的事件来阻止该事件,并调用 stopPropagation( ),所以它不会起泡到 HTMLPanel


I have a simple GWT setup for testing:

<g:HTMLPanel ui:field="container" width="500px" height="300px">
    <g:Label ui:field="inner" text="hello"></g:Label>
</g:HTMLPanel>

With handlers added:

container.addDomHandler(new ClickHandler()
{
    @Override
    public void onClick(ClickEvent event)
    {
        Window.alert("click on container");
    }
}, ClickEvent.getType());

inner.addDomHandler(new ClickHandler()
{
    @Override
    public void onClick(ClickEvent event)
    {
        Window.alert("click on inner");
    }
}, ClickEvent.getType());

Each of these addHandler calls, as we know from the sources,

DOM.sinkEvents(Element elem, int eventBits)

inside, and the latter, according to docs, "Sets the current set of events sunk by a given element. These events will be fired to the nearest {@link EventListener} specified on any of the element's parents."

But in fact, if you click on inner div the click will be fired to its parent div in any of the 3 ways:

  • if I have not set any ClickHandlers on child

  • if I have set handlers on child

  • if I have set the handlers, but cleared the sinking of ClickEvent with

DOM.sinkEvents(inner.getElement(), DOM.getEventsSunk(getElement()) & ~Event.getTypeInt(ClickEvent.getType().getName()));

Why this happens and what is the true role of sinking the events? Why adding a Handler is related to sinking events, i.e. to firing the same event to parent of the given element? Is it possible to prevent all handlers, added to the given widget from calling without removing them explicitely? I.e. is there a way to stop processing all events on some widget?

解决方案

First, sinkEvent is about adding the native event handler on the element, as explained in the GWT wiki.

What you're seeing is event bubbling, which is how events work in browsers: there are 2 phases when dispatching an event: the capture phase (not implemented in old IEs) allows any ancestor element to catch the event before it reaches its target, then in the bubbling phase the event bubbles up the DOM hierarchy. So if you listen for clicks on the HTMLPanel, you'll also receive the bubbling click events from the Label.

You can prevent that by listening to the event at the Label level and calling stopPropagation() so it doesn't bubble up to the HTMLPanel.

这篇关于GWT sinkEvent功能如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆