Messaging

Contains static functions for publishing / subscribing to messages by topic.

Summary
Contains static functions for publishing / subscribing to messages by topic.
Invokes all listeners subscribed to topic.
Attaches a listener to a message topic--Use returned handle to unsubscribe.
Removes a specific subscribed listener from a message topic.
Retrieves the MessagingTopic object associated with the message topic--If topic doesn’t exist, this function will generate one.

Functions

publish

mojo.Messaging.publish = function(topic,
messageObj)

Invokes all listeners subscribed to topic.

Parameters

topic{string} Topic
messageObj{object} Message Object (optional)

Example

// subscribe an object's function to a message topic
var test = {dialog: function(msg) {
if (!msg) msg = "test";
alert(msg);
}}

var handle = mojo.Messaging.subscribe("someTopic", test, "dialog");

// invoke dialog function by publishing message topic
mojo.Messaging.publish("someTopic");

// invoke dialog function, and pass in a parameter
mojo.Messaging.publish("someTopic", ["hello world"]);

subscribe

mojo.Messaging.subscribe = function(topic,
targetObj,
targetFunc)

Attaches a listener to a message topic--Use returned handle to unsubscribe.

Parameters

topic{string}
targetObj{object}
targetFunc{string}

Returns

{object} handle

Example

// subscribe an object's function to a message topic
var test = {dialog: function(msg) {
if (!msg) msg = "test";
alert(msg);
}}

var handle = mojo.Messaging.subscribe("someTopic", test, "dialog");

// invoke dialog function by publishing message topic
mojo.Messaging.publish("someTopic");

// invoke dialog function, and pass in a parameter
mojo.Messaging.publish("someTopic", ["hello world"]);

unsubscribe

mojo.Messaging.unsubscribe = function(handle)

Removes a specific subscribed listener from a message topic.

Parameters

handle{object}

Example

// subscribe to a message topic
var handle = mojo.Messaging.subscribe("someTopic", test, "dialog");
// unsubscribe from message topic
mojo.Messaging.unsubscribe(handle);

getTopic

mojo.Messaging.getTopic = function(topic)

Retrieves the MessagingTopic object associated with the message topic--If topic doesn’t exist, this function will generate one.

Paramters

topic{string}

Returns

{object} Topic

Example

// stand-alone usage
var topicObj = mojo.Messaging.getTopic(“someTopic”);

// usage in a Mojo Controller
this.addObserver(mojo.Messaging.getTopic(“someTopic”), “onPublish”, “SomeCommand”);
mojo.Messaging.publish = function(topic,
messageObj)
Invokes all listeners subscribed to topic.
mojo.Messaging.subscribe = function(topic,
targetObj,
targetFunc)
Attaches a listener to a message topic--Use returned handle to unsubscribe.
mojo.Messaging.unsubscribe = function(handle)
Removes a specific subscribed listener from a message topic.
mojo.Messaging.getTopic = function(topic)
Retrieves the MessagingTopic object associated with the message topic--If topic doesn’t exist, this function will generate one.