hi all -- i can get around the problem below (with $(document)), but i'm trying to figure this out for future knowledge and understanding of jQuery binding and scope.
i have a basic module, and i want to bind something like ajaxStart to it.
- var MyModule = (function($, my) {
- $(my).ajaxStart(function() { window.alert("ajax start"); } );
- return my;
- }(jQuery, MyModule || {}));
- $(document).ready(function() { $.ajax(SOME_AJAX_SETTINGS); } );
the alert never appears.
if i instead bind ajaxStart(...) to $(document), the alert appears.
my guess is that there's some scoping issue here i'm not aware of, but i'm also VERY green with javascript, so i'm having trouble tracking down what might be wrong here.
i do otherwise routinely bind events to non-DOM objects, usually private objects inside the MyModule object, so really i'm just wondering why i can't seem to bind this particular event to a non-DOM object.
EDIT: i wrote up an even simpler example, with two alternative binding patterns, but it appears only "custom" events are sent to non-DOM objects, as neither binding pattern below works:
- var x = {};
- $(x).ajaxStart(function() { window.alert("pattern 1"); } );
- $(x).bind("ajaxStart", function() { window.alert("pattern 2"); } );
- $.ajax(SOME_AJAX_SETTINGS_HERE);
again, if i replace 'x' above in the binding steps with any present DOM element, the alerts fire.
i'm really surprised by this, since ultimately the ajax events are "custom" in the sense that they aren't DOM-triggered events, so i'd have expected them to broadcast to all bound objects, not just the bound DOM objects.
the "Ajax Events" API page does say the events are broadcast to all DOM objects, but i think an explicit warning should be entered into the docs to indicate that other objects will not receive notice of the event.