DOM Events"

From Documentation
m (correct highlight (via JWB))
 
(23 intermediate revisions by 3 users not shown)
Line 2: Line 2:
  
 
__TOC__
 
__TOC__
There are two kinds of events at the client side: widget events (<javadoc directory="jsdoc">zk.Event</javadoc>) and DOM events (<javadoc directory="jsdoc">jq.Event</javadoc>). A widget event is triggered by a widget or an application, while a DOM event is triggered by the browser.
 
  
= Widget Events =
+
A DOM event (<javadoc directory="jsdoc">jq.Event</javadoc>) is the DOM-level event that is usually triggered by the browser. It is usually listened by the implementation of a widget, rather than the client application.
A widget event is the widget-level event. Like <javadoc>org.zkoss.zk.event.Event</javadoc> at the server side, the widget event is usually triggered to a widget (and an application) to notify a widget-level event, such as a window has been moved.
 
  
A widget event will be ''propagated'' to the parent widget, parent's parent and so on, until stopped (<javadoc method="stop(_global_.Map)" directory="jsdoc">zk.Event</javadoc>). Then, if the event is required by the server, it will be sent to the server, and converted to an instance of <javadoc type="interface">org.zkoss.zk.au.AuRequest</javadoc> at the server<ref>For more information, please refer to the [[ZK Client-side Reference/Communication/AU Requests|AU Requests]] section.</ref>.
+
Since ZK Client Engine can intercept most DOM events and encapsulate them into the widget events, it is suggested to listen the widget events, if possible, for better performance (by overriding the corresponding methods, such as <javadoc method="doClick_(zk.Event)" directory="jsdoc">zk.Widget</javadoc>). For more information, please refer to [[ZK Client-side Reference/Notifications/Widget Events|the previous section]].  
  
To fire a widget event, you could invoke <javadoc method="fire(_global_.String, zk.Object, _global_.Map, int)" directory="jsdoc">zk.Widget</javadoc> or <javadoc method="fireX(zk.Event, int)" directory="jsdoc">zk.Widget</javadoc>.
+
= How to Listen and Unlisten =
  
To listen a widget event, you could invoke <javadoc method="listen(_global_.Map, int)" directory="jsdoc">zk.Widget</javadoc>.
+
There are two different approaches to listen a DOM event: <javadoc method="domListen_(_global_.DOMElement, _global_.String, zk.Object)" directory="jsdoc">zk.Widget</javadoc> and jQuery (<javadoc directory="jsdoc">_global_.jq</javadoc>).
  
= DOM Events =
+
==Use domListen_ and domUnlisten_ ==
A DOM event (Event) is the DOM-level event that is usually triggered by the browser. It is usually listened by the implementation of a widget, rather than the client application.
 
  
== How to Listen and Unlisten ==
+
<javadoc method="domListen_(_global_.DOMElement, _global_.String, zk.Object)" directory="jsdoc">zk.Widget</javadoc> registers a DOM-level event listener. The registration should be done when a widget is bound to DOM elements, i.e., when <javadoc method="bind_(zk.Desktop, zk.Skipper, _global_.Array)" directory="jsdoc">zk.Widget</javadoc> is called. It is important to un-register by the use of <javadoc method="domUnlisten_(_global_.DOMElement, _global_.String, zk.Object)" directory="jsdoc">zk.Widget</javadoc> when a widget is un-bound from DOM elements, i.e., when <javadoc method="unbind_(zk.Skipper, _global_.Array)" directory="jsdoc">zk.Widget</javadoc> is called. For example,
  
===Override Particular Methods===
+
<source lang="javascript">
===Use jQuery===
+
bind_: function () {
 +
    this.$supers('bind_', arguments);
 +
    this.domListen_(this.getNode(), "onChange");
 +
},
 +
unbind_: function () {
 +
    this.domUnlisten_(this.node, "onChange");
 +
    this.$supers('unbind_', arguments);
 +
},
 +
_doChange: function (evt) { //event listener
 +
    //evt is an instance of jq.Event
 +
},
 +
</source>
  
== When to Listen and Unlisten ==
+
Unlike jQuery's event listener (<javadoc directory="jsdoc">_global_.jq</javadoc>), <javadoc method="domListen_(_global_.DOMElement, _global_.String, zk.Object)" directory="jsdoc">zk.Widget</javadoc> will be ignored if the widget is under control of ZK Weaver (a WYSIWYG editor), i.e., in the so-called ''Design Mode''. In most cases, a widget should not register any event listeners when it is under control of ZK Weaver to avoid any conflict.
 +
 
 +
==Use jQuery==
 +
 
 +
The use of jQuery (<javadoc directory="jsdoc">_global_.jq</javadoc>) is similar except using one of the event listening methods found in [http://api.jquery.com/category/events/ jQuery].
 +
 
 +
<source lang="javascript">
 +
bind_: function () {
 +
    this.$supers('bind_', arguments);
 +
    jq(this.$n("form")).bind("reset", this.proxy(this._resetForm));
 +
},
 +
unbind_: function () {
 +
    jq(this.$n("form")).unbind("reset", this.proxy(this._resetForm));
 +
    this.$supers('unbind_', arguments);
 +
},
 +
_resetForm: function (evt) { //event listener
 +
  this.doSomething(); //this refers to the widget since this.proxy is used
 +
},
 +
</source>
 +
 
 +
where we use <javadoc method="proxy(_global_.Function)" directory="jsdoc">zk.Object</javadoc> to proxy a function such that <code>this</code> will refer to the widget when the method is called. Also notice that the event name used with jQuery does not start with <code>on</code>.
  
 
=Version History=
 
=Version History=

Latest revision as of 07:45, 18 January 2022


A DOM event (Event) is the DOM-level event that is usually triggered by the browser. It is usually listened by the implementation of a widget, rather than the client application.

Since ZK Client Engine can intercept most DOM events and encapsulate them into the widget events, it is suggested to listen the widget events, if possible, for better performance (by overriding the corresponding methods, such as Widget.doClick_(Event)). For more information, please refer to the previous section.

How to Listen and Unlisten

There are two different approaches to listen a DOM event: Widget.domListen_(DOMElement, String, Object) and jQuery (jq).

Use domListen_ and domUnlisten_

Widget.domListen_(DOMElement, String, Object) registers a DOM-level event listener. The registration should be done when a widget is bound to DOM elements, i.e., when Widget.bind_(Desktop, Skipper, Array) is called. It is important to un-register by the use of Widget.domUnlisten_(DOMElement, String, Object) when a widget is un-bound from DOM elements, i.e., when Widget.unbind_(Skipper, Array) is called. For example,

bind_: function () {
    this.$supers('bind_', arguments);
    this.domListen_(this.getNode(), "onChange");
},
unbind_: function () {
    this.domUnlisten_(this.node, "onChange");
    this.$supers('unbind_', arguments);
},
_doChange: function (evt) { //event listener
    //evt is an instance of jq.Event
},

Unlike jQuery's event listener (jq), Widget.domListen_(DOMElement, String, Object) will be ignored if the widget is under control of ZK Weaver (a WYSIWYG editor), i.e., in the so-called Design Mode. In most cases, a widget should not register any event listeners when it is under control of ZK Weaver to avoid any conflict.

Use jQuery

The use of jQuery (jq) is similar except using one of the event listening methods found in jQuery.

bind_: function () {
    this.$supers('bind_', arguments);
    jq(this.$n("form")).bind("reset", this.proxy(this._resetForm));
},
unbind_: function () {
    jq(this.$n("form")).unbind("reset", this.proxy(this._resetForm));
    this.$supers('unbind_', arguments);
},
_resetForm: function (evt) { //event listener
   this.doSomething(); //this refers to the widget since this.proxy is used
},

where we use Object.proxy(Function) to proxy a function such that this will refer to the widget when the method is called. Also notice that the event name used with jQuery does not start with on.

Version History

Last Update : 2022/01/18


Version Date Content
     



Last Update : 2022/01/18

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.