Publish/Subscribe Communication
While there are plenty of times when the direct "chained" style
of communication provided by dojo.connect is exactly what you'll need to
solve a problem, there are also a lot of times when you'll want a much
more indirect "broadcast" style of communication in which various
widgets communicate anonymously. For these circumstances, you might
instead use dojo.publish and
dojo.subscribe.
A classic example is a JavaScript object that needs to
communicate with other objects in a one-to-many type relationship.
Instead of setting up and managing multiple dojo.connect connections for what seems like
one cohesive action, it's considerably simpler to have one widget
publish a notification that an event has transpired (optionally
passing along data with it) and other widgets can subscribe to this
notification and automatically take action accordingly. The beauty of
the approach is that the object performing the broadcast doesn't need
to know anything whatsoever about the other objects—or even if they
exist, for that matter. Another classic example for this kind of
communication involves portlets—pluggable
interface components (http://en.wikipedia.org/wiki/Portlet) that are managed
within a web portal, kind of like a dashboard.
Tip
The OpenAjax Hub (http://www.openajax.org/OpenAjax%20Hub.html), which you'll read more about in Chapter 4, calls for publish/subscribe communication to be used as the vehicle for effectively employing multiple JavaScript libraries in the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access