简体   繁体   English

如何在Backbone中使JQuery进度栏显示多个Ajax调用的进度?

[英]How do I make a JQuery Progress Bar in Backbone show progress for multiple Ajax calls?

Edit: 编辑:

I can't even get a simple setup working with backbone js such that in the controller 我什至无法使用骨干js进行简单的设置,例如在控制器中

initialize: function() {

    // alert("1"); 
    $( "#progressbar" ).html("0%");
    // alert("2");

    $.ajax({
      dataType:"json", 
      async:true,
      url:"http://localhost:8888/chathau5/rest/tasks/list",
      success: function(data, response) {

      }
    });


    // alert("3");
    $( "#progressbar" ).html("25%");
            // it works when i put the alerts in...

},

any ideas? 有任何想法吗?


I am trying to place a jquery progress bar upon loading. 我试图在加载时放置一个jquery进度栏。 My current structure is java and backbone. 我当前的结构是Java和骨干网。

When i come into the backbone controller, I want to initialise the progress bar, and after each ajax call update the value like the following 当我进入骨干控制器时,我想初始化进度条,并在每次ajax调用之后更新值,如下所示

    initialize: function() {

    $("#progressbar").progressbar({ 
        value: 0
    });

    var tasks = $.ajax({
      dataType: "json",
      async:false,
      url: "http://localhost:8888/chathau5/rest/tasks/list",
      success:function(data,response) {
        tsks = data;
      }
    });

    $("#progressbar").progressbar({ 
        value: 50
    });

    var tasks2 = $.ajax({
      dataType: "json",
      async:false,
      url: "http://localhost:8888/chathau5/rest/tasks/list2",
      success:function(data,response) {
        tsks = data;
      }
    });

    $("#progressbar").progressbar({ 
        value: 100
    });

In my index.html i have But the progress bar does not show at all.....unless i wrap alerts around each $progress bar update.... 在我的index.html中,但是进度条根本不显示.....除非我在每个$ progress bar更新周围包裹警报。

In my backend java i am running the thread sleep for testing purposes... 在我的后端Java中,我出于测试目的运行线程睡眠。

if i put alerts around each progress bar update in the above, it works... i am not sure how this is happening... any ideas how to fix this 如果我在上面的每个进度条更新周围放置警报,它将起作用...我不确定这是如何发生的...有关如何解决此问题的任何想法

Your progressbar code needs to go inside the success function 您的progressbar代码需要放入success函数中

var tasks = $.ajax({
      dataType: "json",
      async:false,
      url: "http://localhost:8888/chathau5/rest/tasks/list",
      success:function(data,response) {
        tsks = data;
        $("#progressbar").progressbar({ 
          value: 50
        });
      }
    });

The issue is most likely caused by the fact that when you call jQuery.ajax with the async:false option, the AJAX call blocks the GUI thread for the duration of the execution, so the browser can't update the progress bar. 该问题很可能是由于以下事实引起的:当您使用async:false选项调用jQuery.ajax时,AJAX调用会在执行期间阻止GUI线程,因此浏览器无法更新进度条。 You should make your AJAX calls asynchronous, and update the progress bar after each step has succeeded: 您应该使AJAX调用异步,并在每个步骤成功之后更新进度条:

The simple way: 简单的方法:

$("#progressbar").progressbar({ 
    value: 0
});

//make first call
var tasks = $.ajax({
  dataType: "json",
  url: "http://localhost:8888/chathau5/rest/tasks/list",
  success:function(data,response) {

    //first call succeeded
    tsks = data;
    $("#progressbar").progressbar({ 
        value: 50
    });

    //make second call
    var tasks2 = $.ajax({
      dataType: "json",
      url: "http://localhost:8888/chathau5/rest/tasks/list2",
      success:function(data,response) {

        //second call succeeded
        tsks = data;
        $("#progressbar").progressbar({ 
            value: 100
        });
      }
    });
  }
});

This solution is far from perfect. 该解决方案远非完美。 It still makes the two AJAX requests sequentially, which is sub-optimal compared to making the requests concurrently. 它仍然顺序地发出两个AJAX请求,与同时发出请求相比,这是次优的。 Maybe something like: 也许像这样:

var $progressbar = $("#progressbar");
var progress = 0;
var updateProgress = function(amount) {
  $progressbar.progressbar({value:progress+=amount});
};

updateProgress(0);

$.ajax({
  dataType:"json", 
  url:"http://localhost:8888/chathau5/rest/tasks/list",
  success: function(data, response) {
    updateProgress(50);
  }
});

$.ajax({
  dataType:"json", 
  url:"http://localhost:8888/chathau5/rest/tasks/list2",
  success: function(data, response) {
    updateProgress(50);
  }
});

First, when something like an alert is helping, it's usually because you have a concurrency bug, meaning that something is finishing or not finishing before something else. 首先,当警报之类的东西有帮助时,通常是因为您有一个并发错误,这意味着某件事先完成还是未完成。 They're really hard to detect. 他们真的很难被发现。 I would guess that the #progressbar is not actually on the screen when initialize gets called. 我猜想初始化调用时#progressbar实际上不在屏幕上。 I could do a quick console.log($("#progressbar")); 我可以做一个快速的console.log($(“#progressbar”)); on the first line of initialize, just to be sure it's actually there. 在初始化的第一行上,只是要确保它确实存在。

Secondly, this is something of an aside but it might help you understand how concurrency errors can take place. 其次,这是一个问题,但它可以帮助您了解如何发生并发错误。 We have no guarantee that the first ajax call will finish first. 我们不能保证第一个ajax调用将首先完成。 That's the asynchronous nature of ajax, we give up control to our callbacks at that point and then we continue with the rest of the method. 那是ajax的异步特性,我们在那时放弃对回调的控制,然后继续进行该方法的其余部分。 So your initialize method is probably finishing, before either of those two success methods get reached. 因此,在达到这两种成功方法之一之前,您的初始化方法可能已完成。 For that reason it's best to nest the two calls. 因此,最好嵌套两个调用。

// I like keeping my display code separated out like this.
this.updateProgress = function( newValue )
{
  $("#progressbar").progressBar({value:newValue}); 
}

$.ajax({ // first call
  success: function() // done loading when this gets called
  {
    this.updateProgress(50);
    $.ajax({ // second call
      success: function() // all done loading at this point
      {
        this.updateProgress(100);
      }  
    });
  }
});

That way, you can be sure that one loads after another. 这样,您可以确保一个接一个地加载。 No reason not to do it at the same time like in your example, but this way we have a guarantee. 没有理由不像您的示例那样同时执行此操作,但是通过这种方式,我们可以保证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM