1

I am building a Mobile Web Application with JQM. It's pretty basic, the front page displays a list of linked items. When a user clicks a link, JQM Dynamically Injects the page showing item specific details. The problem is, when the user clicks a link, i need the app to display the loading animation while JQM fetches the page via ajax and displays it. This is the code:

// Listen for any attempts to call changePage().
$( document ).bind( "pagebeforechange", function( e, data ) {

    // We only want to handle changePage() calls where the caller is
    // asking us to load a page by URL.
    if ( typeof data.toPage === "string" ) {
        // We are being asked to load a page by URL, but we only
        // want to handle URLs that request the data for a specific
        // item.
        var u = $.mobile.path.parseUrl( data.toPage ),
            re = /^#item_/;

        if ( u.hash.search(re) !== -1 ) {
            // Show the loading message
            $.mobile.showPageLoadingMsg();
            // We're being asked to display the information for a specific item.
            // Call our internal method that builds the content for the category
            // on the fly based on our in-memory category data structure.
            showPostDetails( u, data.options );

            // Make sure to tell changePage() we've handled this call so it doesn't
            // have to do anything.
            e.preventDefault();
        }
    }
});

// Load the data for a specific item, based on
// the URL passed in. Generate markup for the items in the
// ajax call, inject it into an embedded page, and then make
// that page the current active page.
function showPostDetails( urlObj, options ) {
    // Get the data string
    var IDString = urlObj.hash.replace( /.*show_id=/, "" )
    // Perform ajax to get the other page details
    $.ajax({
        type: 'POST',
        dataType : 'json',
        async: false,
        url: template_url + '/bin/aj_ax.php',
        data: 'post_id=' + IDString + '&ajax-action=get_post_details',
        success: function( data ) {
            // If we are successful
                    // Hide loading message
                    $.mobile.hidePageLoadingMsg();
            if( data.success ) {
                var $page = $( '#item_' ),
                    // Get the header for the page.
                    $header = $page.children( ":jqmData(role=header)" ),
                    // Get the content area element for the page.
                    $content = $page.children( ":jqmData(role=content)" ),
                    // Get the footer area element for the page.
                    $footer = $page.children( ":jqmData(role=footer)" ),
                    // The markup we are going to inject into the content
                    // area of the page.
                    markup = data.content;
                // Find the h1 element in our header and inject the data
                // header attribute to it.
                $header.find( "h1" ).html( data.header );
                // Inject the markup into the content element.
                $content.html( markup );
                // Inject the footer markup
                $footer.html( data.footer );
                // Pages are lazily enhanced. We call page() on the page
                // element to make sure it is always enhanced before we
                // attempt to enhance the markup we just injected.
                // Subsequent calls to page() are ignored since a page/widget
                // can only be enhanced once.
                $page.page();
                // We don't want the data-url of the page we just modified
                // to be the url that shows up in the browser's location field,
                // so set the dataUrl option to the URL for the category
                // we just loaded.
                options.dataUrl = urlObj.href;
                // Now call changePage() and tell it to switch to
                // the page we just modified.
                $.mobile.changePage( $page, options );
                // Refresh the page elements
                $( "div[data-role=page]" ).page( "destroy" ).page();
            }
        }
    });
}

The strange thing is, the loading message displays just fine when i use Firefox on Win7. However, i get nothing when i use Google Chrome for Windows and Android, the defailt Android Browser and Safari for the iOS devices. Please let me know what i could be doing wrong. Thank you!

EDIT: When i tried to figure out where the problem was, i realized the Loading Animation disappears right when i call $.mobile.changePage( $page, options ); Could there be anything wrong there?

realnsleo
  • 709
  • 2
  • 12
  • 29

2 Answers2

1

I had the exact same problem, for some reason there is a delay when you call the function. This is what I did to get around it:

$('html').addClass( "ui-loading" );

You can stop the loading spinner in the usual way:

$.mobile.hidePageLoadingMsg()
courtland
  • 11
  • 4
0

I had similar problem, and solved it by using beforeSend in Ajax configuration and putting show loading message there.

$.ajax({
    type: 'POST',
    dataType : 'json',
    async: false,
    url: template_url + '/bin/aj_ax.php',
    data: 'post_id=' + IDString + '&ajax-action=get_post_details',
    beforeSend: function () {
        $.mobile.showPageLoadingMsg();
    }
    success: function( data ) {
        $.mobile.hidePageLoadingMsg();
    }

...and try to comment $.mobile.showPageLoadingMsg() inside your pageBeforeChange

micadelli
  • 2,482
  • 6
  • 26
  • 38
  • hi @micadelli thank you for your reply. I have tried this solution with is much elegant, however it does not work still on Chrome for windows and android, as well as android and iphone and ipad browsers. Please check out the site here: http://tradelinksafrica.biz/m. I hope it will give you a better idea of what i am trying to achieve. Thank you! – realnsleo Jun 19 '12 at 10:49
  • After further investigation, there seems to be a huge delay when showing the loading message on Chrome and Phone browsers. This causes the page to display before actually showing the loading. Anyone? Help? – realnsleo Jun 19 '12 at 12:48