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.
Now dispatchEvent runs asynchronously after the current code execution is finished, including menu.
A custom event can be created using the event constructor, like this: const myEvent = new Event('myevent', { bubbles: true, cancelable: true, composed: false }) In the above snippet, we created an event, myevent , by passing the event name to the Event constructor.
While other platforms, like Android or iOS, clearly state what a library is, what format it must have and how it should be integrated into a project, JavaScript doesn't provide any built-in mechanism.
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.
Check out bean. It's awesome.
I would also recommend taking a look at js-signals
Here is a basic example from their site:
//custom object that dispatch a `started` signal
var myObject = {
started : new signals.Signal()
};
function onStarted(param1, param2){
alert(param1 + param2);
}
myObject.started.add(onStarted); //add listener
myObject.started.dispatch('foo', 'bar'); //dispatch signal passing custom parameters
myObject.started.remove(onStarted); //remove a single listener
Try RxJS.
This exposes power of Reactive Extensions and Linq in the Javascript. Example:
this.searcher = $(me._textboxSelector)
.toObservable("keyup")
.Select(function (_) {
return $(me._textboxSelector).val();
})
.Where(function (str) {
if (me._madeSomeHiding && str.length < me._minStringLength) {
$(me._itemsSelector).show();
}
This allows creating a filter on a list. So you can say if user typed 2 characters and stopped for 250ms then do something.
I had the same problem back in a day so I created a super tiny library to help me with events. Check it out, maybe you will find it useful.
https://github.com/anvk/Events.js
var events = new utils.Events();
var callback1 = function() {...};
var callback2 = function(arg1, arg2) {...};
events.on('event A', callback1);
events.on('event B', callback2);
events.emit('event A');
events.emit('event B', [var1, var2]);
A JavaScript event library for everything jQuery events offers without the jQuery.
https://github.com/AtheistP3ace/jAwn
Old question but here is something I put together for personal use but I basically took jQuery and made a custom build of only their events stuff. Stripped out Sizzle and everything else they force on you even with a custom event build.
That leaves you with only the specific event code and the caching (which is needed for events). This code has been updated with all the latest 3.0 jQuery updates and bug fixes. This can only be guaranteed to work with whatever browsers jQuery 3.0 supports (https://jquery.com/browser-support/). All in all the min file is 13kb and supports everything jQuery can do with events. Namespaces, custom events, direct and delegated events, passing data, triggering events, you name it.
Read the docs. It's pretty simple. Works just like jQuery only instead of calling on, off, trigger and the such on a jQuery object you pass the element(s) as the first argument.
As I said before I did this for personal use so its not some project I constantly support and update but I try to keep it up to date for my own uses and make sure it has the latest fixes. I have been using it for years in multiple projects and its worked great. I only recently put it on GitHub in case others found it useful.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With