Last Updated: February 25, 2016
·
474
· netzzwerg

Simple Mediator

define(function(){
"use strict";

var channels = {};
var mediator = {
    subscribe : function(channel, fn){
        if(!channels[channel]) channels[channel] = [];
        channels[channel].push({ callback : fn });
    },
    publish : function(channel){
        var currChan = channels[channel];
        if(!currChan) return false;
        for(var i = 0,l = currChan.length; i < l; i++){
            var subscription = currChan[i];
            subscription.callback(arguments[1]);
        }
    }
};

return mediator;

});