I’m looking for a JavaScript library that will allow me to use custom events that I can subscribe to and fire. I also need the event name/scope to work similarly to that of topics in a message queue, where you can subscribe to a namespace and get all events for that namespace.
For example,
var myCustomEventHandler = new CustomEventHandler();
myCustomEventHandler.bind('my.event', function(data) { console.log('Event 1'); });
myCustomEventHandler.bind('my.other.event', function(data) { console.log('Event 2'); });
myCustomEventHandler.bind('my.*', function(data) { console.log('Event 3'); });
myCustomEventHandler.trigger('my.event');
// Logs "Event 1" and "Event 3"
myCustomEventHandler.trigger('my.other.event');
// Logs "Event 2" and "Event 3"
myCustomEventHandler.trigger('my.something.else');
// Logs "Event 3"
I could write something custom, but I’d prefer to use an open source library if there is one.
Cheers.
YUI 2 has something like that, I assume YUI 3 does too, but I haven’t looked at it in enough detail to know yet. EventEmitter appears to cover at least some of your requirements, and is much smaller. Some of the other libraries on microjs events may be promising too.