View-to-view communication
The ability to define views and define controllers that manipulate the views individually is important; however, often it is also necessary that one view communicates an event to another view and the controller for that view. In many Model-View-Controller (MVC) applications, an event that occurred in one view controller might need to send an event to other view controllers so that all appropriate views are updated.Communication between controllers, or more generally between widgets, is done in one of two ways in the Dojo Toolkit. The first method is through the dojo.connect Application Programming Interfaces (APIs). Using these APIs, you can link a callback to any event in JavaScriptTM, whether it is a typical event like onclick, or a call to any JavaScript function on any JavaScript object. The dojo.connect APIs are robust; however, they require that you explicitly link every function reference. Therefore, the dojo.connect API might not be efficient to use for view-to-view communication. In addition, connecting events can be expensive and lower performance. Therefore, this appoach is most efficiently used for internal widget and internal view communication.
The second, and more efficient way to do view-to-view anonymous communication, is with the Dojo topic package. This package implements a message bus where Dojo widgets can publish messages. Widgets can also subscribe to topics and provide a callback for the JavaScript object to call whenever a message is published to that topic. This process is represented in the following diagram:

Therefore, when
communicating between views in your application, define
topic names that you want to use for the data bus. To define a
topic, specify a topic name. You do not have to
register the topic name before you can use it. When you
decide on the name, you can make calls to publish to that name:
dojo.publish(<topic_name>, <javascript_array>);
To subscribe to the topic name and be notified on message publishing, the application code must register a callback handler for the topic. To register, call the API
dojo.subscribe(<topic_name>, <object_reference_that_contains_the_callback_method>, <callback_method_name>);
Example usage:
Dojo Toolkit topics are a simple, robust, efficient, and flexible way to complete view-to-view communication. For code examples using the topic subsystem of the Dojo Toolkit, refer to the example application.
dojo.publish(<topic_name>, <javascript_array>);
To subscribe to the topic name and be notified on message publishing, the application code must register a callback handler for the topic. To register, call the API
dojo.subscribe(<topic_name>, <object_reference_that_contains_the_callback_method>, <callback_method_name>);
Example usage:
Publisher, widget A: |
Assume widget A is a publisher
to the topic
'fooTopic'. If widget A decides to publish to the
topic, then widget A makes the API call, dojo.publish("fooTopic",
[{ source: this, message: "foo"}]); That API
call
tells the Dojo Toolkit to publish a JavaScript
object that contains an attribute named source, which is a reference to
the posting widget, and an attribute named message, with the string
value of foo, to a
topic named fooTopic.
You do not need an attribute named source, nor an attribute named message. The item posted to the topic can be any type of JavaScript object, atomic or a complex object, which is a collection of attributes. |
Consumer, widget B: |
Assume widget B is a consumer of
messages from fooTopic.
Widget B needs to register the method on its object that acts as the
callback
handler. For example, widget B
wants its function, _gotMessage
called whenever
a message is published to the topic, fooTopic. Widget B
must initiate the following API call in its postCreate Dojo API function: dojo.subscribe("fooTopic",
this, "_gotMessage");
After that call is made, widget
B listens for messages on the named topic. Now, when a
message is published to fooTopic
the method, _gotMessage,
is run for widget B. |
Dojo Toolkit topics are a simple, robust, efficient, and flexible way to complete view-to-view communication. For code examples using the topic subsystem of the Dojo Toolkit, refer to the example application.