The Dojo Topic System is utilized here, functioning as a form of global events that are independent of stores.
Any piece of code can publish a "topic," typically in the format of a URL or filesystem path for organization purposes, along with an optional data object or additional arguments in newer versions of Dojo. It is advisable to use the dojo/topic
module instead of dojo/connect
, especially in later versions like 1.7.
// Dojo 1.7+
require(["dojo/topic"], function(topic){
topic.publish("some/topic", "one", "two");
});
// Dojo 1.7 (AMD)
require(["dojo/_base/connect"], function(connect){
connect.publish("foobar", [{
item:"one", another:"item", anObject:{ deeper:"data" }
}]);
});
// Dojo < 1.7
dojo.publish("foobar", [{
item:"one", another:"item", anObject:{ deeper:"data" }
}]);
Other sections of code can subscribe to specific topics by registering callback functions. Once again, it is recommended to utilize dojo/topic
over dojo/connect
, particularly in newer versions such as 1.7.
// Dojo 1.7+
require(["dojo/topic"], function(topic){
topic.subscribe("some/topic", function(){
console.log("received:", arguments);
});
});
// Dojo 1.7 (AMD)
require(["dojo/_base/connect"], function(connect){
connect.subscribe("/foo/bar/baz", function(data){
console.log("i got", data);
});
});
// Dojo < 1.7
dojo.subscribe("/foo/bar/baz", function(data){
console.log("i got", data);
});
Subscribers to a published topic will be notified when the event occurs, and their respective callback functions will execute. The data object provided with the publish operation is passed to each callback function if available.
Note that subscriptions created after a publish event will not trigger callbacks for previous events. Subscriptions respond only at the time of the publishing event.
This GitHub project seems to be using an outdated Dojo version due to the presence of dojo.subscribe
instead of the modern dojo/topic
module. As the latest Dojo release is 1.9, it is recommended to upgrade and utilize the updated functionalities introduced from version 1.7 onwards.
Documentation links for relevant Dojo versions:
dojo.publish
and dojo.subscribe
(applicable for 1.6 and 1.7)
dojo/topic
module (for 1.9, recommended for 1.7+ coding style)
It is highly encouraged to update to the latest Dojo version and utilize the dojo/topic
module with its publish
and subscribe
methods, avoiding the deprecated dojo.publish
and dojo.subscribe
approaches.
Example snippets sourced from documentation references.