jquery mobile lazy load with JSON

jquery mobile lazy load with JSON

Hope someone can help me, I am working on my first jquery mobile app. I am still new on this, I have one index page which includes three data role pages(get category, get posts, get posts content). and using getJSON() to fetch the data from my wordpress website. my problem is the delay of the listview. page appears empty for about 5 seconds then the lists appears in one time. I want the list appears one by one. not waiting for the all list to complete. also I want to know how to do lazy loading for the page. if you have any other solution for my problem. i will be appreciated. hope someone can help me on this. Thanks

  1. <!DOCTYPE html>
  2. <!--[if IEMobile 7 ]>    <html class="no-js iem7"> <![endif]-->
  3. <!--[if (gt IEMobile 7)|!(IEMobile)]><!--> <html class="no-js"> <!--<![endif]-->
  4.     <head>
  5.         <meta charset="utf-8">
  6.         <title>Touch Marketing & Advertising</title>
  7.          <!-- the three things that jQuery Mobile needs to work -->
  8.          
  9.         <link rel="stylesheet" href="js/jquery.mobile-1.4.2.min.css" /> 
  10.         <script src="js/jquery.js" type="text/javascript"></script>
  11.         <script src="js/jquery1.js" type="text/javascript"></script>
  12.         <script src="js/jquery-ui.js" type="text/javascript"></script>
  13.         <script src="js/my_jquery.js" type="text/javascript"></script>
  14.         <script src="js/jquery.mobile-1.4.2.min.js" type="text/javascript"></script>
  15.         <link rel="stylesheet" href="css/style.css" />
  16.         <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  17.         

  18.     
  19.     </head>
  20.     <body>

  21.         <div id="home-page" data-role="page">
  22.             <div id="home-header" data-role="header">
  23.                 <h1><i class="icon-home"></i> Header</h1>
  24.             </div><!-- /#header -->
  25.             
  26.             <div id="home-content" data-role="content">
  27.             <ul data-role="listview" id="category_list" data-filter="true" data-theme= "a" data-inset="true">   </ul>
  28.             </div>
  29.             
  30.             
  31.             <div id="footer" data-role="footer">
  32.                 <h1><i class="icon-coffee"></i> 2014 - Touch Marketing & Advertising</h1>
  33.             </div><!-- /#footer -->
  34.         </div>
  35.         
  36.         <!----------------------------SECOND Page(Posts List)--------------------------------------------------->
  37.         <div id="SecondPage" data-role="page">
  38.             <div id="home-header" data-role="header">
  39.             <a href="#home-page" class="ui-btn-right">Back</a>
  40.                 <h3>Posts List</h3>
  41.             </div><!-- /#header -->
  42.             
  43.             <div  class="ui-content" data-role="content">
  44.             
  45.             <ul data-role="listview" id="posts" data-filter="true" data-inset="true"> </ul>
  46.          
  47.             </div>
  48.             
  49.              <div id="footer" data-role="footer">
  50.                 <h1><i class="icon-coffee"></i> 2014 - Touch Marketing & Advertising</h1>
  51.             </div><!-- /#footer -->
  52.         </div>
  53.         
  54.         
  55.         <!-----------------------------Third Page(Post Content)------------------------------------------------------>
  56.         
  57.         <div id="thirdPage" data-role="page" data-prefetch="true">
  58.             <div id="content-header" data-role="header">
  59.             <a href="#home-page" class="ui-btn-right">Back</a>
  60.                 <h1 id="PostTitle">Post Title</h1>
  61.             </div><!-- /#header -->
  62.             
  63.             <div id="PostContnet" class="ui-content" data-role="content">
  64.            
  65.             </div>
  66.              <div id="footer" data-role="footer">
  67.                 <h1><i class="icon-coffee"></i> 2014 - Touch Marketing & Advertising</h1>
  68.             </div><!-- /#footer -->
  69.         </div>
  70.         
  71.         <!------------- GET JSON URL---------------------------------------->
  72.         <script type="text/javascript" src="http://touch.ly/api/get_category_index/?callback=ShowCategory"></script>
  73.         

  74.     </body>
  75. </html>
Here is my Script







  1. /*---------------------------- This FUNCTION TO GET ALL CATEGORIES-----------------------------------------*/
  2. function ShowCategory(data){
  3. var output = '';
  4. $.each(data['categories'], function(k, val){
  5. output += '<li><a href="#SecondPage" onclick="ShowPosts('+val['id']+')"> \
  6.  '+val['title']+' <span class="ui-li-count">'+val['post_count']+'</span>\
  7.   </a></li>';
  8. });
  9. $('#category_list').append(output);
  10. }
  11. /*--------------------------------------------THIS FUNCTION TO GET CATEGORY POSTS----------------------------*/
  12. function ShowPosts(id){
  13. var postslist = '';
  14. $.getJSON('http://touch.ly/?json=get_category_posts&id=' + id + '&callback=?', function(data){
  15. $.each(data['posts'], function(k, val){
  16. $('#posts').empty();
  17. var input = val['date'];
  18. input = input.split("T")[0]; 
  19. var date = $.datepicker.parseDate("yy-mm-dd", input); 
  20. var newDateString = $.datepicker.formatDate("dd-mm-yy", date);
  21. var Post_title = val['title'];
  22. postslist +=  '<li><a href="#thirdPage" onclick="CatPostContent('+val['id']+')"> \
  23.  '+val['title']+' <br />\
  24.  <p class="ui-li-aside">'+newDateString+'</p></a></li>';
  25.  
  26. $('#posts').append(postslist);
  27. $('#posts').listview('refresh');
  28. });
  29. });
  30. }
  31. /*-------------------------------------GET POSTS CONTENT-----------------------------------------*/
  32. function CatPostContent(id){
  33. $.getJSON('http://touch.ly/?json=get_post&id=' + id + '&callback=?', function(data){
  34. var title= '<h2> '+ data.post.title+' </h2>';
  35. var content = '<p> '+ data.post.content +' </p>'
  36. $('#PostTitle').html(title);
  37. $('#PostContnet').html(content);
  38. console.log(title);
  39. });
  40. }