针对其中纳入AjaxStart /停止特定的Ajax调用 [英] Targeting specific ajax calls which incorporate AjaxStart/Stop

查看:219
本文介绍了针对其中纳入AjaxStart /停止特定的Ajax调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的网页上的Ajax调用,以显示加载微调了。我想针对不同的Ajax调用和显示不同的微调。随着事态的代表,因为ajaxStart是一个全球性的活动,全Ajax调用结束的同时,显示所有纱厂。

这个工程...这增加了类加载到隐藏分区包含微调并显示。

 的$(document).bind(ajaxStart,函数(){
    $ body.addClass(装载);
});

(文档)$ .bind(ajaxStop,函数(){
    $ body.removeClass(装载);
});
 

现在从其他堆栈的答案我已经看到,您可以添加命名空间ajaxstart /停止 (<一href="http://stackoverflow.com/questions/1061922/jquery-should-i-use-multiple-ajaxstart-ajaxstop-handling">jQuery我应该使用多个ajaxStart / ajaxStop处理)

......沿着线

 的$(document).bind(ajaxStart.secondCall,函数(){
    $ body.addClass(loading2);
});

(文档)$ .bind(ajaxStop.secondCall,函数(){
    $ body.removeClass(loading2);
});
 

但是,这并不在其目前的形式工作。任何指导,将AP preciated ...

更新:

作为附加内容ILYAS解决方案,并根据他的指导下我实现了AJAX开始FUNCITONALITY在我的Ajax调用....

 函数sendResponse(thread_id单){
        $(文件).off(reference_call。);
         $(文件)。在(ajaxStart.secondCall,函数(){
         $('微调')显示()。
      });
 $阿贾克斯({
    网址:someURL.php,
    类型:'后',
    数据: {
              //数据
         },
            成功:功能(数据){
            $(文件)。在(ajaxStop.secondCall,函数(){
               $('微调')隐藏()。
            });
                    //做的东西....

                    } 其他 {

                     //做的东西....
                    }
              }
        });
  返回false;
}
 

解决方案

所以,当有人提到在链接到问题的讨论,你可以结合并使用您的自定义命名空间的AJAX事件取消绑定。下面是简单的例如证明了这种方法。在您的code中的第一部分绑定与 .firstCall 命名空间的AJAX事件,像这样的:

  $(文件)。在(ajaxStart.firstCall,函数(){
    $('。spinner1)显示()。
});
$(文件)。在(ajaxStop.firstCall,函数(){
    $('。spinner1)隐藏()。
});
//然后去你的Ajax调用
 

后来在code,当你需要发送第二阿贾克斯不同的微调,则必须解除绑定 .firstCall 命名空间,并结合 .secondCall 是这样的:

  $(文件).off(新华美通);
$(文件)。在(ajaxStart.secondCall,函数(){
    $('。spinner2)显示()。
});
$(文件)。在(ajaxStop.secondCall,函数(){
    $('。spinner2)隐藏()。
});
//然后进入第二个Ajax调用
 

作为一种替代方法,你可以考虑使用一组全局AJAX事件处理程序和移动你的逻辑是什么微调器来显示/隐藏的回调里面,但是这真的取决于采用的逻辑。

I am trying to display a loading spinner for during ajax calls on my page. I want to target different ajax calls and display different spinners. As things stands, as ajaxStart is a global event, an all ajax calls end up displaying all spinners at the same time.

This works...it adds the class "loading" to a hidden div containing the spinner and displays it.

$(document).bind("ajaxStart", function () {
    $body.addClass("loading");
});

$(document).bind("ajaxStop", function () {
    $body.removeClass("loading");
});

Now from other stack answers I have seen that you can add namespaces to ajaxstart/stop (jQuery should I use multiple ajaxStart/ajaxStop handling)

......along the lines of

$(document).bind("ajaxStart.secondCall", function () {
    $body.addClass("loading2");
});

$(document).bind("ajaxStop.secondCall", function () {
    $body.removeClass("loading2");
});

But this doesn't work in its current form. Any guidance would be appreciated...

UPDATE:

AS AN ADDITION TO ILYAS SOLUTION AND BASED ON HIS GUIDANCE I IMPLEMENTED THE AJAX START FUNCITONALITY IN MY AJAX CALL ....

function sendResponse(thread_id){
        $(document).off(".reference_call");
         $(document).on("ajaxStart.secondCall", function () {
         $('.spinner').show();
      });
 $.ajax({
    url: 'someURL.php',
    type: 'post',
    data: {
              //data
         },
            success: function(data) {
            $(document).on("ajaxStop.secondCall", function () {
               $('.spinner').hide();
            });
                    //do stuff....

                    } else {

                     //do stuff....
                    }
              }
        });
  return false;
}

解决方案

So, as it was mentioned in the discussion linked to the question, you can bind to and unbind from ajax events using your custom namespaces. Here is simple example demonstrating this approach. In the first part of your code you bind to ajax events with .firstCall namespace, like this:

$(document).on("ajaxStart.firstCall", function () {
    $('.spinner1').show();
});
$(document).on("ajaxStop.firstCall", function () {
    $('.spinner1').hide();
});
// then goes your ajax call

Later in code, when you need to send second ajax with different spinner, you must unbind from .firstCall namespace and bind to .secondCall like this:

$(document).off(".firstCall");
$(document).on("ajaxStart.secondCall", function () {
    $('.spinner2').show();
});
$(document).on("ajaxStop.secondCall", function () {
    $('.spinner2').hide();
});
// then goes your second ajax call

As an alternative you might consider using one set of global ajax events handlers, and move your logic on what spinner to show/hide inside those callbacks, but this really depends on what logic that is.

这篇关于针对其中纳入AjaxStart /停止特定的Ajax调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆