Skip to content

Instantly share code, notes, and snippets.

@minwe
Forked from zpao/gist:8344371
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minwe/284ba2bc599fef37d7c0 to your computer and use it in GitHub Desktop.
Save minwe/284ba2bc599fef37d7c0 to your computer and use it in GitHub Desktop.
var CustomEvents = (function() {
var _map = {};
return {
subscribe: function(name, cb) {
_map[name] || (_map[name] = []);
_map[name].push(cb);
},
notify: function(name, data) {
if (!_map[name]) {
return;
}
// if you want canceling or anything else, add it in to this cb loop
_map[name].forEach(function(cb) {
cb(data);
});
}
}
})();
// in <MyComponent>
CustomEvents.subscribe('foo', function(data) {
console.log('foo', data);
});
// in <SomeOtherComponent>
CustomEvents.notify('foo', {bar: 7});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment