- The Book of Dojo
- Quick Installation
- Hello World
- Debugging Tutorial
- Introduction
- Part 1: Life With Dojo
- Part 2: Dijit
- Part 3: JavaScript With Dojo and Dijit
- Part 4: Testing, Tuning and Debugging
- Part 5: DojoX
- The Dojo Book, 0.4
Publish and Subscribe Events
Submitted by MattBowen on Tue, 06/05/2007 - 18:37.
In addition to the simple event system created by dojo.connect, dojo offers support for anonymous publication and subscription of objects, via dojo.publish and dojo.subcribe. These methods allow a function to broadcast objects to any other function that has subscribed. This is dojo's topic system, and it makes it very easy to allow separate components to communicate without explicit knowledge of one another's internals.
There are three functions that you need to understand to use dojo's topic system: dojo.publish, dojo.subscribe, and dojo.unsubscribe Dojo.publish calls any functions that are connected to the topic via dojo.subscribe, passing to those subscribed functions arguments that are published (see syntax for details). As one might expect, dojo.unsubscribe will cause a previously subscribed function to no longer be called when dojo.publish is called in the future
How does it work?
Imagine that you run a running a conference, and there will be updates throughout the day. You could collect contact information for everyone at the beginning of the day, along with each person's interests. However, this would be a lot of logistical work. Instead, you decide to use your facility's Public Address System. When there is an update to the schedule, you announce "This is an update to the schedule: the Dojo training is full and we have added yet a third time slot for it tomorrow." When there is meal information, you announce "This is an update about food: we will be serving free ice cream in the main hall in five minutes." This way, anyone interested in your information can pay attention to any updates that could change their behavior. You don't need to know who is subscribing, and they don't need to fill out a bunch of paper work — it's a win-win.
Syntax
dojo.publish
dojo.publish(Topic Name [string], Arguments to Pass to Subscribed Function [array])
dojo.subscribe
handle = dojo.subscribe(Topic Name [string], Context of Linked Method [string or null], Linked Method [string or function])
dojo.unsubscribe
dojo.unsubscribe(Handle [handle object])
Example Code for Reference
function globalGuy(arg) { console.debug("Global Guy fired with arg " + arg); }
var someObject = {
bar: function(first, second) { console.debug("Bar fired with first of "+first+" and second of "+second); return 7; },
}
}
var someObject = {
bar: function(first, second) { console.debug("Bar fired with first of "+first+" and second of "+second); return 7; },
}
}
Subscribing and Publishing Topics
To connect globalGuy to the topic "globalEvents" and someObject.bar to "fullNames", you simply use dojo.subscribe, as follows:
topics = [];
topics[0] = dojo.subscribe("globalEvents", null, globalGuy);
topics[1] = dojo.subscribe("fullNames", "someObject", bar);
topics[0] = dojo.subscribe("globalEvents", null, globalGuy);
topics[1] = dojo.subscribe("fullNames", "someObject", bar);
Note that the following alternative form would also work:
topics = [];
topics[0] = dojo.subscribe("globalEvents", globalGuy);
topics[1] = dojo.subscribe("fullNames", "someObject", "bar");
topics[0] = dojo.subscribe("globalEvents", globalGuy);
topics[1] = dojo.subscribe("fullNames", "someObject", "bar");
To publish information to both of these topics, you pass dojo.publish the topic names and arrays of the arguments that you want to pass to subscribed functions, as follows
dojo.publish("globalEvents", ["data from an interesting source"]);
dojo.publish("fullNames", ["Alex", "Russell"]);
dojo.publish("fullNames", ["Alex", "Russell"]);
To disconnect someObject.bar from its topic, you use dojo.disconnect, as follows:
dojo.unsubscribe(topics[1]);
- Printer-friendly version
- Login or register to post comments
- Unsubscribe post
2nd arg to dojo.subscribe method shd not be passed as String
When i tried the above example in Firefox the following error was thrown
In dojo.js.uncompressed.js the signature of the subscribe method is
So replaced the String "someObject" with the object someObject and it worked.
One more correction
It seems that the signature should be:
dojo.subscribe = function(/*String*/ topic, /*Object|null*/ context, /*String*/ method)
When I tried entering the function name without quotes FireFox threw an unrecognized function error - put it in quotes and ouila! It worked.