Question

I'm using JS render to create dynamic pages in Jquery Mobile for my Phonegap app. The problem however is that the listview isn't loaded until the page is refreshed. Clearly that's not what I want and in another app I used the exact same technique and the listview is loaded immediately. Thing is that there's no difference between the two, so I have no idea why it isn't working in this one.

Here's my script for loading the pages:

<script src="jquery/jsrender.min.js"></script>
<script type="text/javascript">
$(document).on('pageinit', '#kunstwerken_exp1', function (event, ui) {
    var $page = $(event.target); 
    $.ajax({
        dataType:"json",
        url:"json/exp1_index.js",
        success:function(data, textStatus, jqHXR){
            for( var x = 0; x < data.length; x++){
                //create valid unique IDs for each page.
                data[x].id = data[x].link.replace("?","").replace("=","").replace("#","");
            }
            console.log("success:");
            console.log(data);
            $("#templateDropPoint").html($("#template").render(data));
            $("#templateDropPoint").listview("refresh");
            $("body").append($("#pagetemplate").render(data));

        },
        error:function(jqXHR, textStatus, errorThrown ){
            console.log(textStatus+ " "+ errorThrown);
        }
    });

});
</script>

This is the HTML:

<div data-role="page" id="kunstwerken_exp1" data-url="kunstwerken_exp1">
<div data-role="content">   
    <ul data-role="listview" id="templateDropPoint"></ul>
</div><!-- /content -->
</div>

<script id="template" type="text/x-jsrender">
<li>
<a href="#{{>id}}">
  <img src={{>img}} />
  <h5><b>{{>naam}}</b></h5>
  <h6>{{>kunstwerk}}</h6>
</a>
</li>
</script>

<script type="text/x-jsrender" id="pagetemplate">
<div data-role="page" id="{{>id}}">
    {{if pagina}}
    <div data-role="content">
        <h1>{{>naam}} ({{:pagina.nationaliteit}})</h1>
        <h2>{{:pagina.kunstwerk}}</h2>
        <em>{{:pagina.onderschrift}}</em>

        <div class="ui-grid-a my-breakpoint">
            <div class="ui-block-a">
            <div class="koloma">
                <div class="callbacks_container">
                <ul class="rslides">
                    <li id="callbacks1_s0">
                    <img src="{{:pagina.afbeelding1}}" alt=""><p class="caption">{{:pagina.onderschrift1}}</p>
                    </li>
                </ul>
                </div>
                {{if pagina.videooff}}
                <ul class="tabs">
                    <li><a href="#" onclick="clickKunstenaar0();"><img src="images/video.png"/></a></li><br>
                </ul>
                {{/if}}
            </div>
            </div>

        <div class="ui-block-b">
        <div class="kolomb">{{:pagina.tekst}}</div>
        </div>
        </div>  

    </div>
    {{/if}}
</div>
</script>
Was it helpful?

Solution

To understand this situation you need to understand how jQuery Mobile works. It uses AJAX to load other pages.

First page is loaded normally. Its HEAD and BODY is loaded into the DOM, and they are there to await other content. When second page is loaded, only its BODY content is loaded into the DOM. To be more precise, even BODY is not fully loaded. Only first div with an attribute data-role="page" will be loaded, everything else is going to be discarded. Even if you have more pages inside a BODY only first one is going to be loaded. This rule only applies to subsequent pages, if you have more pages in an initial HTML all of them will be loaded.

That's why your listview is show successfully only after a page refresh.

Here's an official documentation: http://jquerymobile.com/demos/1.2.0/docs/pages/page-links.html

Unfortunately you are not going to find this described in their documentation. Ether they think this is a common knowledge or they forgot to describe this like my other topics. (jQuery Mobile documentation is big but lacking many things).

I have another ANSWER that discusses this problem. Solutions and working examples can be found there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top