开发者

jQuery ajax:before event

开发者 https://www.devze.com 2023-02-02 03:48 出处:网络
I\'ve seen some coworkers use this in rails $(\'[data-once=true]\').live(\'ajax:before\', function(request, e) {

I've seen some coworkers use this in rails

    $('[data-once=true]').live('ajax:before', function(request, e) {

});
开发者_StackOverflow中文版

However I'm trying to use it with jquery and it doesnt work, does this event comes with the rails jquery adapter?


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();
});


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.


This 'ajax:before' isn't a useful event for .live() to handle, unless you've defined a custom event with that name.

0

精彩评论

暂无评论...
验证码 换一张
取 消