Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show Page Loading Spinner on Ajax Call in jQuery Mobile

I'm using $.ajax() to populate a list in my mobile web app. What I'd like to do is have the jQuery mobile loading spinner appears while this call is being performed and disappear once the list populates. The current version of JQM uses $.mobile.showPageLoadingMsg() and $.mobile.hidePageLoadingMsg() to show and hide the loading spinner, respectively. I can't figure out where exactly to place these statements to get the correct result. This seems like it should be a fairly easy thing to accomplish, I just haven't been able to find anything about this exact scenario.

Here's the ajax call inside the pagecreate function:

$('#main').live('pagecreate', function(event) {         $.ajax({             url: //url             dataType: 'json',             headers: //headers             success: function(data) {                 for(i = 0; i < data.length; i++) {                     $('#courses').append('<li>' + data[i].name + '<ul id="course' + data[i].id + '"></ul>' + '<span class="ui-li-count">' + data[i].evaluatedUserIds.length + '</span></li>');                     $('#course' + data[i].id).listview();                     for(j = 0; j < data[i].evaluatedUserIds.length; j++) {                         $('#course' + data[i].id).append('<li><a href="">' + data[i].evaluatedUserIds[j] + '</a></li>');                     }                     $('#course' + data[i].id).listview('refresh');                 }                 $('#courses').listview('refresh');             }         });     }); 
like image 890
Sean Avatar asked Aug 26 '11 17:08

Sean


2 Answers

You can use the beforeSend and complete events of $.ajax to call $.mobile.showPageLoadingMsg and $.mobile.hidePageLoadingMsg. Would look like this:

$('#main').live('pagecreate', function(event) {         $.ajax({             beforeSend: function() { $.mobile.showPageLoadingMsg(); }, //Show spinner             complete: function() { $.mobile.hidePageLoadingMsg() }, //Hide spinner             url: //url             dataType: 'json',             headers: //headers             success: function(data) {                 //...             }         });     }); 
like image 94
Alex Turpin Avatar answered Sep 21 '22 03:09

Alex Turpin


Before JQM 1.2:

$(document).ajaxStart(function() {     $.mobile.showPageLoadingMsg(); });  $(document).ajaxStop(function() {     $.mobile.hidePageLoadingMsg(); }); 

Since JQM 1.2:

$(document).ajaxStart(function() {     $.mobile.loading('show'); });  $(document).ajaxStop(function() {     $.mobile.loading('hide'); }); 

http://api.jquerymobile.com/page-loading/

like image 43
Marvin Emil Brach Avatar answered Sep 23 '22 03:09

Marvin Emil Brach