Created
March 3, 2015 16:57
-
-
Save mauriciosoares/a044091b592be646843d to your computer and use it in GitHub Desktop.
Mediator
This file contains hidden or 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 subscriptions = {}; | |
var Mediator = function() {}; | |
Mediator.prototype.subscribe = function(key, callback, context) { | |
subscriptions[key] = subscriptions[key] || []; | |
subscriptions[key].push({ | |
callback: callback, | |
context: context || window | |
}); | |
}; | |
Mediator.prototype.publish = function(key, params, context) { | |
if(!subscriptions[key]) return; | |
var length = subscriptions[key].length; | |
for(var i = 0; i < length; i += 1) { | |
subscriptions[key][i].callback.call(subscriptions[key][i].context, params); | |
} | |
}; | |
NS.mediator = new Mediator(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment