Home

Awesome

Medium JS Build Status devDependency Status

A small library that implements the Mediator Pattern in JavaScript. The essence of the Mediator pattern is to "Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently". Read more

Medium makes use of the Mediator pattern and the difference of the Mediator pattern over the Observer pattern is that a single object is responsible for communication. Read more

Installation

$ npm install mediumjs

$ bower install mediumjs

$ component install pazguille/mediumjs

See: https://github.com/component/component

How-to

First, requires the mediator instance:

var medium = require('mediumjs');

Now, defines a listener and subcribes to a channel:

function informal(arg1, arg2) {
    alert(arg1);
    alert(arg2);
});

medium.subscribe('greet', informal);

Then, broadcasts a channel with some data:

medium.publish('greet', 'Hi pazguille!', 'Bye pazguille!');

API

Medium#subscribe(channel, listener)

Adds a listener to given channel.

medium.subscribe('greet', informal);

Medium#publish(channel, [arg1], [arg2], [...])

Execute each item in the listener collection in order with given parameters.

medium.publish('greet', 'Hi pazguille!', 'Bye pazguille!');

Medium#remove(channel, listener)

Removes one or all listeners from the collection with given channel.

// removes a specific listener from the given channel
medium.remove('greet', informal);

// removes all listeners and the channel
medium.remove('greet');

Maintained by

License

Licensed under the MIT license.

Copyright (c) 2013 @pazguille.