javascript - global custom events in jQuery -


I want to use custom events independent of DOM elements, but I'm not sure how to get the best approach is .

Here's what I started:

  // Some system components register an event handler $ (). Bind ("foo.bar", handler); // foo is my app's namespace, bar is the name of the event // the second part of the system closes $ $. Trigger ("foo.bar", {context: "lorem ipsum"});  

After looking at the source of jQuery, especially with the management of global AJAX events, I thought it should work:

  $ Fn.bind ("foo .bar", handler); // ... $ .event.trigger ("foo.bar", {references: "leerem ipsum"}); However, it appears that my handler function is never called.  

Am I going to go wrong about this?

If you type jQuery & gt; 1.4, , then a blank is a collection of JQuery which will mean that an event handler is not really anything. jquery (document) before 1.4 .

Add this only global name space (a real object) and then add event to it:

  var FOO = {}; $ (FOO) .bind ("foo.bar", handler); $ (FOO). Trigger ("foo.bar", {references: "Laurem ipsum"});  

Comments