The Dojo Publish/Subscribe Event Mechanism | 2 | WebReference

The Dojo Publish/Subscribe Event Mechanism | 2


[prev]

The Dojo Publish/Subscribe Event Mechanism [con't]

Who Uses the Topic Publish/Subscribe Mechanism

Now that you know what the topic publish/subscribe mechanism is, you'll find that it's used throughout Dojo. For instance, drag and drop functionality uses the channels /dnd/move/start and /dnd/move/end to publish information about drag operations, and the developer is intended to subscribe to these as needed. The Cometd client also implements a variation of publish/subscribe/unsubscribe for it's communication. Cometd (originally call "Shortbus") is an attempt to standardize Comet as a protocol with a reference implementation. The protocol, named Bayeux, runs on top of the server push to allow pub sub subscribe to events, sort of like a JMS for JavaScript. You can learn more about the Comet and server push in my Comet Programming: Using Ajax to Simulate Server Push article. There are several Dijit widgets that use publishing to alert the user and other widgets about state changes. For instance, any time a new Tab is added to a TabContainer, a message is sent out on a channel based on the ID of the Tabs.

Likewise, we can publish events for our own widgets. In the Loading Dynamic HTML Content Using the Dijit ContentPane article, we saw how the dijit.layoutContentPane notifies interested parties of certain events like onDownloadStart() and onDownloadEnd(). I wanted to see a similar announcing of changes to the content property so I created my own ContentPane subclass which overrides the _setContentAttr() method. In it, I added a call to dojo.publish() with the widget's ID, along with the old and new content. A call to this.inherited(arguments) executes the existing code. Subscribers can then do what they wish with the passed data:

In the declarative widget constructor, the content is set within a dojo/method script because setting it as a tag attribute in the defining <DIV> tag doesn't trigger the setter. That only happens when the widget's set() method is called:

Here is what is printed to the console when we run the above example:

Content changed:
Widget ID: MyContentPanel
Old content: undefined
New content: Hello from the ContentPane

Notice that the old content is undefined because there was none assigned during widget parsing.

Here's a file that contains the source code for all the examples that we looked at today.

Conclusion

Dojo's topic publish/subscribe mechanism is a great way for objects or widgets to send out anonymous broadcasts where subscribers don't have to know about the publisher's inner workings, or existence, for that matter. It's simple to use, flexible, and extensible too. In a future article, we'll be looking at Ben Nolan's dojo.behavior JavaScript library. Until then, here's some resources that you might find useful:

References:


Have a suggestion for an article topic? Do you have a product or service that you'd like reviewed? Email it to Rob .


resides in Ottawa, Canada, and is the founder of GravelleConsulting.com. Rob has built systems for Intelligence-related organizations such as Canada Border Services, CSIS as well as for numerous commercial businesses. Rob to receive a free estimate on your software project. Should you hire Rob and his firm, you'll receive 15% off for mentioning that you heard about it here!

In his spare time, Rob has become an accomplished guitar player, and has released several CDs. His former band, Ivory Knight, was rated as one Canada's top hard rock and metal groups by Brave Words magazine (issue #92). MP3s of his covers project are available from iTunes and other digital music sites.

Rob uses and recommends MochaHost, which provides Web Hosting at $3.10 per month, 2 LifeTime Free Domains, and 6 Months Free!

Original: April 23, 2010


[prev]