Although dojo. connect provides a kind of direct action/ reaction style of communication, the publish/subscribe metaphor has many highly applicable use cases in loosely coupled architectures in which it's not prudent for objects or widgets to know about one another's existence.

This is simple enough to set up. The dojo.publish function accepts a topic name and an optional Array of parameters that should be passed to any subscribers. Becoming a subscriber to a topic is done through the dojo.subscribe function, which accepts a topic name and a function that is executed in response to the published topic. Here's a working example with a couple of Function Objects:

Name:  Pub sub communication.jpg
Views: 84
Size:  96.2 KB

function Faa (topic) {
this. greet = function () console .log ("Hi, I'm Faa");
/* Foo directly publishes information, but not to anywhere specific ... * / dojo. publish ("!l j / salutation") ;
subscribes to information, but not from anywhere specific * /
dojo. subscribe (" / lj / salutation", this, "greet");

A couple of variations on the pub/sub metaphor are available, but the vanilla dojo.publish/dojo.subscribe functions relay the general idea. Any situation in which you cannot (for whatever reason) expose an API might be a prime opportunity to take advantage of pub/sub communication in your application.