A simple PubSub in JavaScript
JavaScript
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
examples
src code cleanup + optimized code size Jan 13, 2014
tests simplified implementation: not supporting namespaces in events anymore Jan 13, 2014
.gitignore added gruntfile + code + specs Dec 27, 2013
LICENSE Update LICENSE Jan 13, 2014
README.md Update README.md Jan 13, 2014
bower.json added bower.json for bower registration Jan 13, 2014
gruntfile.js added "npm test" script + added repository to package.json Jan 5, 2014
package.json added "npm test" script + added repository to package.json Jan 5, 2014
pubsub.min.js code cleanup + optimized code size Jan 13, 2014

README.md

PubSub

A simple, small (under 1kb minified) PubSub implementation in JavaScript. It is completely dependency-free and can be used as a RequireJS module.

How to install

It you use bower, simply install the component with it:

bower install pubsub

and then include the script file in yout html:

<script src="bower_components/pubsub/pubsub.min.js"></script>

Without bower just clone/download this repository and include the script file in your html:

<script src="pubsub.min.js"></script>

How to use it

Without RequireJS

window.PubSub.subscribe('test', function(a, b) {
  console.log(a); // 1
  console.log(b); // 2
  console.log(this.event); // "test"
  console.log(this.args); // [1, [2, 3, 4]]
});

window.PubSub.publish('test', 1, [2, 3, 4]);

With RequireJS

define(['pubsub'], function(PubSub) {
  PubSub.subscribe('test', function(a, b) {
    console.log(a); // 1
    console.log(b); // [2, 3, 4]
  });
});

define(['pubsub'], function(PubSub) {
  PubSub.publish('test', 1, [2, 3, 4]);
});

API

The API is very simple.

/* Subscribes a function to a specific event.
 * The function receives the event data object[s] as arguments.
 * The scope of the handler function contains event and args. {event: event, args: [data, ...]}
 * @param {string} event
 * @param {function} fn
 */
PubSub.subscribe(event, fn)

/* Publishes an event with the supplied message data object[s].
 * @param {string} event
 * @param {anything} data - any number of event data objects.
 */
PubSub.publish(event [, data, data, data...])

/* Unsubscribes an event handler from the specified event.
 * If event is missing, the handler will be unsubscribed from all subscribed events.
 * @param {string} event - optional
 * @param {function} handler
 */
PubSub.unsubscribe([event], handler)

There are also some shorter aliases for convenience

PubSub.sub(event, fn)
PubSub.pub(event [, data, data, data...])
PubSub.unsub([event], handler)

License

This code is licensed under the MIT license. If you like it, please drop a line.