Search code examples
jquery-mobilejquery-mobile-listviewjquery-mobile-ajax

Dymanic listview construction visible in jQuery Mobile 1.4.2 transition


I have a Single Page transition from #homepage to #addresses where the page #addresses include a dynamic listview build based in an $.ajax WebApi call.

The problem is that it's visible the construction of the listview when we arrive at the second page, and I want to avoid that, I want the list all build when we land in the #addresses page.

I also have a delay click in the listview in iPhone.

My code:

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>izigo.mobile</title>
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.css">
    <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
    <script>

        $(document).bind("mobileinit", function ()
        {
            $.mobile.toolbar.prototype.options.addBackBtn = true;
            $.mobile.toolbar.prototype.options.backBtnText = "voltar";
            $.mobile.page.prototype.options.domCache = true;
        });

    </script>
    <script src="http://code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
    <script>
        /* Pass data with changePage */
        $(document).on("pageinit", "#homepage", function ()
        {
            $(".category").on("click", function ()
            {
                $.mobile.pageContainer.pagecontainer("change", "#addresses",
                    {
                    categoryId: this.id,
                    transition: "slide"
                });
            });
        });

        /* retrieve data and run function to add elements */
        $(document).on("pagebeforechange", function (e, data)
        {
            if (data.toPage[0].id == "addresses")
            {
                var categoryId = data.options.categoryId;

                clearListCategory("#addresses");
                buildListCategory("#addresses", categoryId);
            }
        });

        function clearListCategory(page)
        {
            var $page = $(page);

            $("ul", $page).remove();
        }

        function buildListCategory(page, categoryId)
        {
            $.ajax({
                type: "POST",
                url: "http://10.0.0.200/api/Mobile/GetAddresses",
                crossDomain: false,
                beforeSend: function () { $.mobile.loading('show') },
                complete: function () { $.mobile.loading('hide') },
                data: { CategoryId: categoryId },
                dataType: 'json',
                success: function (addresses)
                {
                    showAddresses(page, addresses);
                },
                error: function () {
                    console.log("loadList error!");
                }
            });
        }

        function showAddresses(page, addresses)
        {
            var $page = $(page);

            var list = $("<ul/>", {
                "data-role": "listview"
            });

            var items = '';

            $.each(addresses, function (i, address)
            {
                items = $("<li>" + address.Name + "</li>");
                list.append(items);
            });

            $(".ui-content", $page).append(list);
            $("ul", $page).listview();
        }
    </script>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

    <!-- home-page -->
    <div data-role="page" id="homepage">

        <div data-role="header" data-position="fixed"><h1>mobile</h1></div>

        <div class="ui-content" role="main">

            <ul data-role="listview" id="categories">
                <li><a href="#" id="3" class="category">Oficina</a></li>
                <li><a href="#" id="100" class="category">Seguro</a></li>
                <li><a href="#" id="101" class="category">Reboque</a></li>
            </ul>

        </div>

    </div>

    <!-- page addresses list -->
    <div data-role="page" id="addresses">

        <div data-role="header" data-position="fixed"><h1>mobile</h1></div>

        <div class="ui-content" role="main"></div>

    </div>

</body>
</html>

Solution

  • The code you're using, you populate listview on pagebeforechange event, which is triggered before any other page event. You should populate listview before you navigate to target page, using .success or .complete callback.

    $.ajax({
        type: "POST",
        url: "URL",
        crossDomain: false,
        beforeSend: function () {
            $.mobile.loading('show')
        },
        complete: function () {
            $.mobile.loading('hide')
        },
        data: {
            CategoryId: categoryId
        },
        dataType: 'json',
        success: function (addresses) {
            showAddresses(page, addresses);
            $.mobile.pageContainer.pagecontainer("change", "#addresses");
        },
        error: function () {
            console.log("loadList error!");
        }
    });
    

    Demo - Code