简体   繁体   English

jQuery中的全局自定义事件

[英]global custom events in jQuery

I want to use custom jQuery events independent of DOM elements, but I'm not sure what the best way is to achieve this. 我想使用独立于DOM元素的自定义jQuery事件,但我不确定实现这一目标的最佳方法是什么。

Here's what I started out with: 这是我开始的:

// some system component registers an event handler
$().bind("foo.bar", handler); // foo is my app's namespace, bar is event name
// another part of the system fires off the event
$().trigger("foo.bar", { context: "lorem ipsum" });

After looking at jQuery's source, in particular its handling of global AJAX events, I figured this should work: 在查看了jQuery的源代码,特别是它对全局AJAX事件的处理之后,我认为这应该可行:

$.fn.bind("foo.bar", handler);
// ...
$.event.trigger("foo.bar", { context: "lorem ipsum" });

However, it appears that my handler function is never even called. 但是,似乎我的处理函数永远不会被调用。

Am I perhaps going about this the wrong way? 我可能会以错误的方式解决这个问题吗?

If you're using jQuery >1.4 then $() returns an empty jQuery collection which would mean that no event handler is actually bound to anything. 如果你使用的是jQuery >1.4那么$()返回一个空的jQuery集合,这意味着没有事件处理程序实际绑定到任何东西。 Before 1.4 it would have returned the same as jQuery(document) . 1.4之前,它将返回与jQuery(document)相同的jQuery(document)

It might be better to simply have a global namespace (an actual object) and then add events to that: 简单地拥有一个全局命名空间(一个实际的对象)然后添加事件可能会更好:

var FOO = {};

$(FOO).bind("foo.bar", handler);

$(FOO).trigger("foo.bar", { context: "lorem ipsum" });

I found my way here because I was looking to implement the publisher/subscriber pattern using namespaced custom events using jQuery. 我找到了自己的方式,因为我希望使用jQuery使用命名空间自定义事件来实现发布者/订阅者模式。 While the accepted solution is a way to use $.event.trigger() in a way that is not tied to DOM elements, it won't work well for a true global event implementation in a publisher/subscriber architecture (such as with a complex UI with many asynchronous actions), where you want to have arbitrary objects/elements listen for a custom event. 虽然接受的解决方案是一种以不依赖于DOM元素的方式使用$ .event.trigger()的方法,但它对于发布者/订阅者体系结构中的真正全局事件实现(例如使用具有许多异步操作的复杂UI),您希望任意对象/元素侦听自定义事件。

Through experimentation, I've found that the real answer to why AnC's events were not firing is because jQuery apparently doesn't allow the "." 通过实验,我发现为什么AnC的事件没有被解雇的真正答案是因为jQuery显然不允许“。” (period) character in custom event names...but underscores seem to be ok. 自定义事件名称中的(句点)字符...但下划线似乎没问题。

So, if you name your events something like foo_bar (rather than foo.bar), your code should work as expected. 因此,如果您将事件命名为foo_bar(而不是foo.bar),则代码应按预期工作。 Tested with jQuery 1.4.4. 使用jQuery 1.4.4进行测试。

Edit: Just to be clear - I mean that periods aren't allowed for custom events if you want to use the $.event.trigger() mechanism. 编辑:只是为了清楚 - 我的意思是如果你想使用$ .event.trigger()机制,不允许自定义事件的句点。 In scenarios where events are being triggered by objects or elements, periods seem to be ok...not sure if this is a bug or by design. 在事件由对象或元素触发的情况下,句点似乎没问题......不确定这是错误还是设计。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM