Create new topics
The PubSub
library includes a number of topics out of the box and these topics are described later in this section.
If you need to create a custom topic, you can do so using the topic()
function, passing in the ID of your topic. The topic()
function returns
an existing topic if the ID already exists. If no topics exist that match the passed ID,
a new topic is created and returned.
Custom topic objects have the publish, publishWith
, subscribe, and unsubscribe functions. Note that custom topics are available for subscription by custom widgets only; default widgets have no knowledge of or access to custom topics. They are used exclusively to let one custom widget know about an event that has happened in another custom widget. Oracle recommends that any custom topics you create include a merchant-specific prefix in the topic ID to avoid conflicts with default topics.
The following example shows the creation of a topic named MY_TOPIC
in a
custom widget:
$.Topic("MY_TOPIC.memory").publish("Message is here") ;
This example shows a subscription to MY_TOPIC
in another custom
widget:
$.Topic("MY_TOPIC.memory ").subscribe(function(message)
{
console.log("Message is: " + message);
});
Note that this custom topic makes use of the .memory
suffix, which enables memory for the topic. Typically, for a callback function to be triggered, it has to be subscribed to a topic before any messages are published to that topic. The .memory
suffix allows a callback function to be triggered for the most recently published message even if the function has subscribed to the topic after the message was published.