Created
May 20, 2013 21:07
-
-
Save tonydub/1a290bdde6ec7ca6d7b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mediator = (function(){ | |
// Storage for topics that can be broadcast or listened to | |
var topics = {}; | |
// Subscribe to a topic, supply a callback to be executed | |
// when that topic is broadcast to | |
var subscribe = function( topic, fn ){ | |
if ( !topics[topic] ){ | |
topics[topic] = []; | |
} | |
topics[topic].push( { context: this, callback: fn } ); | |
return this; | |
}; | |
// Publish/broadcast an event to the rest of the application | |
var publish = function( topic ){ | |
var args; | |
if ( !topics[topic] ){ | |
return false; | |
} | |
args = Array.prototype.slice.call( arguments, 1 ); | |
for ( var i = 0, l = topics[topic].length; i < l; i++ ) { | |
var subscription = topics[topic][i]; | |
subscription.callback.apply( subscription.context, args ); | |
} | |
return this; | |
}; | |
return { | |
publish: publish, | |
subscribe: subscribe, | |
installTo: function( obj ){ | |
obj.subscribe = subscribe; | |
obj.publish = publish; | |
} | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment