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;
});
Written by Béla Varga
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Javascript
Authors
Related Tags
#javascript
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#