new PubSub()
A class provides a scaffolding implementation named Publish–subscribe pattern which represents a messaging pattern.
- Since:
-
- 2.0.0
- See:
Example
var PubSub = caph.require('PubSub'); var publisher = new PubSub(); var Subscriber = function(message, topic) { console.debug(message, topic); } publisher.on('moduleA.topic', Subscriber ); publisher.fire('moduleA.topic', { works : 'ok' } );
Methods
-
fire(topic, message)
-
Publishes the given topic with the given meesage.
Parameters:
Name Type Description topic
string A topic to be published.
message
Object A detailed data of the topic to be published.
- Since:
-
- 2.0.0
Example
var PubSub = caph.require('pattern.PubSub'); var publisher = new PubSub(); var Subscriber = function(message, topic) { console.log('fired'); //this console.log will be called if the fire is successfully } publisher.on('moduleA.topic', Subscriber ); publisher.fire('moduleA.topic', { works : 'ok' } );
-
off(topic, listener)
-
Unsubscribes the given topic and unbind the given function with it.
Parameters:
Name Type Description topic
string An unique string to represent a topic.
listener
callbackTopicPublished A callback function to be invoked when the given topic is published.
- Since:
-
- 2.0.0
Example
var PubSub = caph.require('pattern.PubSub'); var publisher = new PubSub(); var Subscriber = function(message, topic) { console.log(message, topic); } publisher.on('moduleA.topic', Subscriber ); publisher.off('moduleA.topic', Subscriber );
-
on(topic, listener) → {Object}
-
Subscribes the given topic and bind the given function with it.
Parameters:
Name Type Description topic
string An unique string to represent a topic.
listener
callbackTopicPublished A callback function to be invoked when the given topic is published.
- Since:
-
- 2.0.0
Returns:
returns an object that publish the topic with the message
- Type
- Object
Example
var PubSub = caph.require('pattern.PubSub'); var publisher = new PubSub(); var Subscriber = function(message, topic) { console.log(message, topic); } publisher.on('moduleA.topic', Subscriber );