Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom event library for JavaScript. Are there any? Recommendations? [closed]

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.

like image 485
Stephen Melrose Avatar asked May 16 '11 09:05

Stephen Melrose


People also ask

Is dispatchEvent async?

Now dispatchEvent runs asynchronously after the current code execution is finished, including menu.

What are JavaScript custom events?

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.

Does JavaScript have built in libraries?

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.


6 Answers

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.

like image 194
Quentin Avatar answered Oct 02 '22 06:10

Quentin


Check out bean. It's awesome.

like image 37
Johnny Avatar answered Oct 02 '22 04:10

Johnny


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
like image 32
ruyadorno Avatar answered Oct 02 '22 05:10

ruyadorno


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.

like image 24
Aliostad Avatar answered Oct 02 '22 04:10

Aliostad


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]);
like image 44
anvk Avatar answered Oct 02 '22 05:10

anvk


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.

like image 42
AtheistP3ace Avatar answered Oct 02 '22 05:10

AtheistP3ace