Jump To …

fakePublisher.js

lib/publisher/fakePublisher.js v0.5.0
under MIT License
(by) Jan Muehlemann (jamuhl)
   , Adriano Raiano (adrai)

A fake implemetation for publishing. Use only for development purpose.
For production role your own implementation providing a publish function.

var root = this
  , fakePublisher;

if (typeof exports !== 'undefined') {
    fakePublisher = exports;
} else {
    fakePublisher = root.fakePublisher = {};
}

fakePublisher.VERSION = '0.5.0';

create new publisher instance

fakePublisher.createPublisher = function(options) {
    return new Publisher(options);
};

Publisher

var Publisher = function(options) {
    this.published = [];
    this.subscriptions = [];
};

Publisher.prototype = {

subscribe: subscribes for all events.

pub.subscribe(callback)

  • callback: function(event){}
    subscribe: function(callback) {
        this.subscriptions.push({callback: callback});
    },

publish: publishes the event.

pub.publish(event)

  • event:__ the event that must be published
    publish: function(event) {    
        for (var subscriber in this.subscriptions) {
            subscriber.callback(event);
        }
        
        this.published.push(event);
    }

};