Subscribe to a topic

You use the subscribe function to subscribe your callback function to a topic.

The data that has been published to a topic is then passed to the callback function.

There are two ways to use the subcribe() function. You can either provide the callback function’s name or you can provide code for the callback function in-line. In this example, whenever the PAGE_CHANGED topic has a message published to it, the getPageUrlData() function is called.

$.Topic(pubsub.topicNames.PAGE_CHANGED).subscribe(
            widget.getPageUrlData);

In this example, whenever the PAGE_CHANGED topic has a message published to it, the anonymous, in-line function is executed.

$.Topic(pubsub.topicNames.PAGE_CHANGED).subscribe(
     function(value){
        widget.isDisplayErrorPins(false);
     }
 );

Note that the value for this in your callback function may vary, depending on whether the message that triggered the callback function was published with context or not, so you should save the current value of this if you need to guarantee that it remains constant. See Publish messages for more details on publishing messages with context.

To unsubscribe from a topic, use the unsubscribe() function, for example:

$.Topic(pubsub.topicNames.PAGE_LAYOUT_LOADED).unsubscribe(
            widget.resetOrderDetails);