Explain Event System in Dojo ?

Answer

Dojo’s event system offers a refreshing alternative to the normal JavaScript events. With Dojo, you connect functions to one another, creating a link that calls one function when another fires.

dojoAttachEvent

dojoAttachEvent allows you to specify DOM event handlers in a very concise manner:

dojo.webui.widgets.MyWidget = function() {

this.templateString = ‘<div dojoAttachPoint=”divNode”
dojoAttachEvent=”onClick; onMouseOver: onNode;”></div>’;
}

In other words, for a widget that contains the above templateString, there is a div node, that will listen for onclick and onmouseover DOM events. The onclick event will be routed to a handler called onClick for that widget, while the onmouseover event will be routed to handler onNode

dojo.webui.widgets.MyWidget = function() {

this.onClick = function(evt) {
// do something when clicking on the div for this widget
}

this.onNode = function(evt) {
// do something else when mousing over the div for
// this widget
}

}

Note that onNode and onClick are just arbitrary names of methods in the widget. The limitation of dojoAttachEvent is that it only currently works for binding from DOM events.

dojo.event.connect

dojo.event.connect is a mechanism for connecting a dom event or method call to another event handler or method call:

dojo.webui.widgets.MyWidget = function() {

dojo.event.connect(this.divNode, “onclick”, this, “onClick”);
dojo.event.connect(this, “onClick”, this, “onNode”);
dojo.event.connect(widgetBar, “onBar”, this, “onNode”);

}

The first connect method above does the same thing as the example above defined with dojoAttachEvent.
The second one is more interesting, as it connects the onClick handler method to another method called onNode.
The third example shows how you might connect any two method from any two objects.

dojo.event.topic

Say you have a case where you have a number of widgets in a document, and you want them all to be able to listen for the same event, and also push events to the other widgets that are listening. With the normal dojo.event.connect, you would need to create a connection between each set of widgets manually. Instead, you just do the following:

// to send/publish an event to the topic
dojo.event.topic.publish(“recordFound”, newRecord);
// to listen/subscribe to all events published to a topic
dojo.event.topic.subscribe(“recordFound”, this, “onNode”);

disconnect and unsubscribe

To remove an event connection or a topic subscription, dojo.event.disconnect anddojo.event.topic.unsubscribe take exactly the same parameters as their counterparts

All DOJO Questions

Ask your interview questions on dojo

Write Your comment or Questions if you want the answers on dojo from dojo Experts
Name* :
Email Id* :
Mob no* :
Question
Or
Comment* :
 





Disclimer: PCDS.CO.IN not responsible for any content, information, data or any feature of website. If you are using this website then its your own responsibility to understand the content of the website

--------- Tutorials ---