GWT 中的下沉事件--与其他 JS 框架的兼容性
我注意到,在 GWT 的 DOMStandardImpl.java 中,通过在元素上设置 onevent 属性来引用事件调度程序来沉没事件。例如,
protected native void sinkEventsImpl(Element elem, int bits) /*-{
...
if (chMask & 0x00001) elem.onclick = (bits & 0x00001) ?
@com.google.gwt.user.client.impl.DOMImplStandard::dispatchEvent : null;
...
}-*/;
问题是这可能是与现有 JavaScript 代码和其他 JS 框架不兼容的根源。为什么他们使用 elem.onevent=func 方法而不是
elem.addEventListener('event',func,false);
允许开发人员向元素添加多个事件侦听器的首选方法?
谢谢。 特洛伊
I notice that in GWT's DOMStandardImpl.java, events are sunk by setting the onevent properties on the element to refer to an event dispatcher. e.g.,
protected native void sinkEventsImpl(Element elem, int bits) /*-{
...
if (chMask & 0x00001) elem.onclick = (bits & 0x00001) ?
@com.google.gwt.user.client.impl.DOMImplStandard::dispatchEvent : null;
...
}-*/;
The problem with that is that this may be a source of incompatibility with existing JavaScript code and other JS frameworks. Why do they use the elem.onevent=func method as opposed to the preferred
elem.addEventListener('event',func,false);
which would allow the developer to add multiple event listeners to the element?
Thanks.
Troy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
GWT 的 DOMImpl 已经(至少在编写它们时)进行了基准测试,以根据浏览器使用最快的选项;这就是
DOMImplStandard
使用事件处理程序属性的原因(以及DOMImplOpera
没有if chMask & 0x00001)
部分,因为在那里分配onxxx
属性非常快)。至于与其他框架的潜在不兼容性:
wrap()
的元素(还包括RootPanel.get(String)
代码>),但是然后再说一遍,如果事情进展不顺利,你就要承担责任。addEventListener
(或 IE 的attachEvent
)。因此,如果您确实存在不兼容/冲突,请首先责怪自己(见上文),然后责怪 GWT 和您的 JS 库。简而言之:这不是问题。
GWT's
DOMImpl
have been (at least by the time they were written) benchmarked to use the fastest option depending on the browser; this is whyDOMImplStandard
uses event handler properties (and whyDOMImplOpera
doesn't have theif chMask & 0x00001)
part, because assigning theonxxx
property is so fast there).As for the potential incompatibility with other frameworks:
wrap()
inside a widget (that also includesRootPanel.get(String)
), but then again, you're held responsible if things don't play well together.addEventListener
(or IE'sattachEvent
). So if you do have an incompatibility/conflict, first blame yourself (see above), then blame both GWT and your JS lib.In brief: it's a non-issue.