jQuery ajax:before event
Solution 1
Yes, ajax:before
is a jQuery event that rails adds/triggers, though in the latest version it's not ajax:beforeSend
. In jQuery though (ajax:before
was around before the JS framework agnostic changes to rails), you can just attach to this globally, using the ajaxSend
global event, like this:
$(document).bind('ajaxSend', function(e, request, options) {
$('[data-once=true]').something();
});
Solution 2
I'm not a rails dev, so I'm not sure how rails changes this equation, but this is how I'd attach an function to the AJAX before in plain ole' JS:
$.ajax({
beforeSend: function(){
alert("Before");
},
complete: function(){
alert("after");
}
});
Note that these events are fired for all AJAX requests thereafter. You're essentially subscribing to the AJAX object.
See the jQuery AJAX Events Documentation for more info.
Solution 3
This 'ajax:before'
isn't a useful event for .live()
to handle, unless you've defined a custom event with that name.
Related videos on Youtube
ryudice
Updated on June 04, 2022Comments
-
ryudice almost 2 years
I've seen some coworkers use this in rails
$('[data-once=true]').live('ajax:before', function(request, e) { });
However I'm trying to use it with jquery and it doesnt work, does this event comes with the rails jquery adapter?
-
crispy over 13 yearsWhy that? If you dynamically load a remote form, you also want to have the listener functioning for that.
-
user113716 over 13 years@duddle: Don't downvote this answer when it is correct. As I already stated,
'ajax:before'
isn't an event that is useful to.live()
unless you've defined a custom event with that name. I'm not saying that it wouldn't be useful to have a particular event perform a particular task. I'm saying thatlive()
does not recognize that event by default. You need to manually define a custom event. -
user113716 over 13 yearsNick: Are you certain that
ajaxSend
works with.live()
? It works for me with.bind()
only (doesn't mean I'm not missing something). -
crispy over 13 yearsWell, live() does recognize that event by default in Rails. Since you did that statement without respecting the Rails context and without trying to provide a solution, I find your answer not very helpful.
-
user113716 over 13 years@duddle: I'll admit that I glossed over the part about the Rails jQuery adapter. Interesting though that you downvoted mine (which is correct) when one answer makes no attempt to address the
.live()
issue, and the other suggests another event that.live()
doesn't seem to recognize. By the way, it isn't clear if OP is using Rails. Only states that coworkers use it, and has seen that event used withlive()
. Since OP is aware of the adapter, I'd suggest that he's not using Rails. -
crispy over 13 yearsWell, for me it's not about correct but being helpful. The other answers provide a starting point for finding the right solution, are constructive and point to relevant parts in the framework docs or source code. On the one hand, you could be just more descriptive and enable a real knowledge transfer. On the other hand, I expect an answer to include at least one idea of how to proceed in order to find a solution. Enrich your answer so I can remove the downvote. But I also have to agree that the question is not clear about the Rails context and I might have misinterpreted that.
-
Nick Craver over 13 years@patrick - which version of jQuery are you using? global events have changed drastically over various versions.
-
user113716 over 13 yearsNick: Using 1.4.4. Here's an example using
.live()
, and here's an example using.bind()
. Both log to the console onsuccess
, but only thebind
version does so onajaxSend
. -
Nick Craver over 13 years@patrick - You're right on the
ajaxSend
directly, I'll tweak my example above...the way they proxy the trigger it will work with.live()
, didn't really consider that with my original work-around code. -
user113716 over 13 years@duddle: Thanks, but I think I'll leave it. I still think it explains why the code fails. I don't know what the best solution is because the actual situation hasn't been described. The
.live()
method may or may not be the best solution in this case. -
user113716 over 13 yearsNick: "proxy the trigger..." I'll just smile and nod and pretend like I know what you mean. ;o) I'm certain you're correct. +1 on the nice
.live()
workaround. -
Nick Craver over 13 years@patrick - I mean they trigger the event which is know to be global on their elements, which as a result of optimization, makes it unsuitable for
.live()
...back around 1.3.something global events changed completely, rather than firing on all elements, known global events now loop through$.cache
and only fire on elements that subscribe to the event, the same as$.event.trigger()
does: github.com/jquery/jquery/blob/master/src/event.js#L327-333 -
grantwparks about 11 yearsSo plain ole JS means jQuery?