Publish and subscribe messaging system
Describes how the Dojo publish and messaging system is used for components in the Optimizely Content Management System (CMS) user interface.
Optimizely Content Management System (CMS) uses the Dojo publish and subscribe messaging system to pass events or notifications to components in the user interface. The Dojo toolkit facilitates this communication because Dojo promotes class declaration and modular development. The publish and subscribe messaging system is a key mechanism for keeping modules decoupled.
Implement client-side messaging
Publish or subscribe is a well-known pattern for promoting decoupled messaging and communication between independent application building blocks. The decoupling achieved with publish and subscribe results from using the central messaging hub in Dojo and publishing and subscribing on specific topics rather than on specific objects.
For example, consider a widget that displays statistics about the current page from Google Analytics. The widget needs to know when the page changes to update what it displays. Because the available widgets and their ability to change context are unknown, monitoring events on particular objects is not possible. However, a message with the /epi/cms/contextchanged topic gets published on any context change, so the widget subscribes to that topic to update its context.
postCreate: function () {
this._contextChangedHandler = topic.subscribe('/epi/cms/contextchanged', this, this._onContextChange);
},
_onContextChange: function (context, caller) {
// Widget will update itself using the new context.
}This code is invoked whenever another component publishes a message with the same topic.
topic.publish('/epi/cms/contextchanged', [context]);
Add modules to the system and monitor existing events without changing existing code. Many objects can monitor the events of one particular object without that object tracking who is monitoring it, unlike a normal observable pattern.
Public topics
The following topics are available for publish and subscribe messaging:
/epi/cms/requestcontextchange– Requests the application to try to change the context by passing it some information about the context,pageLink, for example, and a reference to the object requesting the change./epi/cms/contextchanged– Occurs when the current context changes. Returns the new context and a reference to the object that originally requested the change./epi/cms/contextchangefailed– Occurs when a context change request fails. Returns the current context, a reference to the object that originally requested the change, and the parameters passed with the context change request.
Updated 17 days ago
