JavaScript的自定义事件库。有没有?建议? [英] Custom event library for JavaScript. Are there any? Recommendations?

查看:105
本文介绍了JavaScript的自定义事件库。有没有?建议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个JavaScript库,这将允许我使用我可以订阅并触发的自定义事件。我还需要事件名称/范围与消息队列中的主题类似,您可以在其中订阅命名空间并获取该命名空间的所有事件。



例如,

  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');
//记录事件1和事件3

myCustomEventHandler.trigger('my.other.event');
//记录事件2和事件3

myCustomEventHandler.trigger('my.something.else');
//记录事件3

我可以写一些习惯,但我会喜欢使用开源库,如果有的话。



干杯。

解决方案>

YUI 2有这样的东西,我认为YUI 3也是,但是我还没有足够详细地了解它。 EventEmitter 似乎至少满足您的一些要求,而且要小得多。 微型事件上的其他一些库可能也是有希望的。


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.

这篇关于JavaScript的自定义事件库。有没有?建议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆