Skip to content

Instantly share code, notes, and snippets.

@luisrudge
Created April 15, 2013 16:37
Show Gist options
  • Save luisrudge/5389406 to your computer and use it in GitHub Desktop.
Save luisrudge/5389406 to your computer and use it in GitHub Desktop.
function MessageBus() {
var self = this;
var subscribable = new ko.subscribable();
self.publish = function (bus, message) {
subscribable.notifySubscribers(message, bus);
};
self.subscribe = function (bus, callback, callbackTarget) {
subscribable.subscribe(function (message) {
callback(ko.toJS(message));
}, callbackTarget || this, bus);
};
}
//how to use it?
var mb = new MessageBus();
mb.publish('way2.message-name', {
prop1: 'value1'
});
mb.subscribe('way2.message-name', function (message) {
console.log(message.prop1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment