1

我正在使用 JQM 构建一个移动 Web 应用程序。这是非常基本的,首页显示链接项目列表。当用户单击链接时,JQM 会动态注入显示项目特定详细信息的页面。问题是,当用户单击链接时,我需要应用程序显示加载动画,而 JQM 通过 ajax 获取页面并显示它。这是代码:

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

奇怪的是,当我在 Win7 上使用 Firefox 时,加载消息显示得很好。但是,当我使用适用于 Windows 和 Android 的 Google Chrome、默认的 Android 浏览器和适用于 iOS 设备的 Safari 时,我什么也得不到。请让我知道我可能做错了什么。谢谢!

编辑:当我试图找出问题所在时,我意识到当我调用 $.mobile.changePage( $page, options ); 时加载动画消失了。那里有什么问题吗?

4

2 回答 2

1

我遇到了完全相同的问题,由于某种原因,您调用该函数时会出现延迟。这就是我为解决它所做的:

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

您可以以通常的方式停止加载微调器:

$.mobile.hidePageLoadingMsg()
于 2012-07-28T00:56:28.280 回答
0

我有类似的问题,并通过beforeSend在 Ajax 配置中使用并在那里放置显示加载消息来解决它。

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

...并尝试$.mobile.showPageLoadingMsg()在您的内部发表评论pageBeforeChange

于 2012-06-19T10:20:19.243 回答