Dojo FAQ: How can I get a list of all topics registered?

By on May 14, 2014 11:36 am

DojoFAQ

Dojo’s topic module provides a singleton object for application-wide publish/subscribe functionality. Pub/sub is a loosely coupled messaging protocol by design. It is designed for scenarios in which publishers and subscribers do not need to know anything about each other. The entire focus is the message — the publisher sends a message without concern about who receives it, and the subscribers await messages, without concern of the sender.

Pub/sub implementations typically do not provide a list of topics that have been published or subscribed to, and Dojo’s implementation conforms to this standard — there is no built-in mechanism to acquire a list of topics. To provide developers a reference to the topics available in your application you should document them at the system level (a higher level than individual module documentation).

Tracking subscriptions and publications

One possibility for programmatically capturing lists of topics is to use Dojo’s aspect module to respond to calls to topic.subscribe and topic.publish. We can listen globally to all activity by providing an aspect.after advising functions for these methods. While the example below simply captures the names of published or subscribed topics, the advising functions could be modified to perform additional logic.

Keep in mind that this will only take effect after this code is executed – any subscriptions or publications made before executing this code would not be captured. In cases where it is absolutely necessary to load a certain module before other modules, an AMD loader plugin is a good solution.

Learning more

We cover dojo/topic, dojo/aspect, advanced AMD usage, and more in depth in our Dojo workshops offered throughout the US, Canada, and Europe, or at your location. We also provide expert JavaScript and Dojo support and development services, to help you get the most from JavaScript and Dojo.

Comments