开发者

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

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines guidelines. It is not currently accepting answers. 开发者_运维技巧

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 5 years ago.

Improve this question

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.


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.


Try KeyderJS:

Keyder(document.getElementById("my_element")).click(function(){
    alert("Mouse button pressed");
}, function(){
    alert("Mouse button released");
});


A little late but you might be interested in https://github.com/asyncly/EventEmitter2

From the description:

FEATURES

Namespaces/Wildcards.
Times To Listen (TTL), extends the once concept with many.
Browser environment compatibility.
Demonstrates good performance in benchmarks


I would also suggest KeyderJS as it is a "Simple yet powerful JavaScript library". I found and still find it very useful. It makes my coding easier.It also has many browser compatibility.

Also you don't have to know JavaScript very good to use it, and you can be a JavaScript newbie or expert to use it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜