So stressed: Template for JSON + Phonegap not fully working, flicker

So stressed: Template for JSON + Phonegap not fully working, flicker

Hey all,

After DAYS of trying to get this to work and no luck I've thought I'll try get some help.

What I'm trying to do:

  • Phonegap + jQuery Mobile app (Done)
  • On selected pages pull html data from my wordpress website using the json API plugin.
  • Steps I want include: Opens app, user navigates to a page, once the page is shown if the device is online start to pull data and show it, if its offline alert the user with a message and have a refresh button to try again (maybe the users 3G dropped out for example).
What i've done
  • Successfully set up phonegap, jQuery mobile and multiple pages on the index.html page.
  • Successfully called  document.addEventListener("deviceready", onDeviceReady, false); etc.
  • At the moment have the particular page getting data from my server BUT there is a issue of going back to the page it will do it again so its shown twice, then 3 times, then 4 times etc.
  • Have a issue with the transition flickering while trying to start the json script on.pageshow more here: https://groups.google.com/forum/?fromgroups#!topic/phonegap/Lbbq_vUFaV0
Issues
  • Transition flickers when loading the page and leaving the page that contains the json, I am using "$(document).on("pageshow", "#pagenamehere", function( event ) {" because I only want the json script to start when the page actually loads.
  • The data loads, but if I leave the page and come back the script activates again, so the data is doubled up, then if I come back triples. So I need a way for it to clear when I leave the page and it reloads when I go back to the page.
  • I am trying to set this up as a template page because I need to repeat it over a number of pages that points to a different post on my site.
  • Maybe my code isn't that great?
My code

  1. <!DOCTYPE html> 
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>s</title>

  6. <link rel="stylesheet" href="css/themes/silver.min.css" />
  7. <link rel="stylesheet" href="jquery/jquery.mobile.structure-1.3.1.min.css" /> 

  8. <style>
  9. #loading
  10.  {
  11.    position:fixed; 
  12.    _position:absolute;
  13.    top: 0;
  14.    left:47%; 
  15.    padding:2px 5px;
  16.    z-index: 5000;
  17.    background-color:#CF4342;
  18.    color:#fff;
  19.  }
  20.  </style>
  21. <script src="jquery/jquery-1.9.1.min.js"></script>

  22. <script type="text/javascript">
  23.  $( document ).bind( "mobileinit", function() {
  24.  
  25.  
  26.         $.support.cors = true;
  27.         $.mobile.allowCrossDomainPages = true;
  28.  
  29.             // Setting #container div as a jqm pageContainer
  30.             $.mobile.pageContainer = $('#content');
  31.  
  32.             // Setting default page transition to slide
  33.             $.mobile.defaultPageTransition = 'slide';
  34.     });
  35. </script>  
  36. <script src="jquery/jquery.mobile-1.3.1.min.js"></script>
  37. <script src="cordova-2.7.0.js" type="text/javascript"></script>
  38. <script src="js/MapKit.js" type="text/javascript"></script>
  39. <script src="js/social.js" type="text/javascript"></script>

  40. <script type="text/javascript" charset="utf-8">
  41.  // Wait for Cordova to load
  42.  
  43.   function onLoad() {
  44.  document.addEventListener("deviceready", onDeviceReady, false);
  45.   document.addEventListener("online", onOnline, false);
  46.   document.addEventListener("offline", onOffline, false);  
  47.     }
  48.    
  49.    function onDeviceReady() {

  50.       $(document).on("pageshow", "#pagenamehere", function( event ) {

  51. if(navigator.network.connection.type == Connection.NONE) {
  52. navigator.notification.alert("Sorry you need to be connected to the internet to access up to date pricing.", function() {}, "You're offline!");
  53. } else {
  54. $.ajax({
  55.         url: 'http://www.myurlhere.com.au/?json=1&custom_fields=nativecontent&callback=?',
  56.    dataType: 'json',
  57.         crossDomain: true,

  58.         success: function(data, status)
  59.   {
  60.   
  61.    (data.portfolio);
  62.   $.each(data.page, function(i,item){
  63.   //alert(i + item.realisation.title);
  64. content = '' + item.nativecontent + '';

  65. $(content).appendTo("#datacontent");

  66.           });
  67.   },
  68.  error: function (xhr, ajaxOptions, thrownError) { 
  69.                    alert(xhr.statusText); alert(thrownError); 
  70.                    }
  71. });
  72.             
  73. }

  74.             });
  75. // alert dialog dismissed
  76. function alertDismissed() {
  77.         // do something
  78. }

  79.     // Show a custom alertDismissed
  80.    
  81.         navigator.notification.alert(
  82.             'Hello.',  // message
  83.             alertDismissed,         // callback
  84.             'Please note',            // title
  85.             'I understand'                  // buttonName
  86.         );
  87.    };
  88.    
  89.  // Social Plugins Check
  90. window.plugins.social.available(function(avail) {
  91.   if (avail) {
  92.     // Show social widgets
  93.   } else {
  94.     // Social not supported
  95.   }
  96. });

  97. </script>
  98. </head>
  99. <body onload="onLoad()">   

  100. <!--Start home page-->
  101. <div data-role="page" id="home" data-prefetch="true">
  102.     
  103. <!--Home Page - Header-->
  104.    
  105.         <div data-role="header" data-position="fixed">
  106.                 <h2>Page Name</h2>
  107.                 <a href="#nav-panel" data-icon="bars" data-iconpos="notext">Menu</a>
  108.                 <a data-icon="info" data-rel="dialog" onClick="window.plugins.social.share('Message goes here.', 'http://www.url.com.au', 'www/image/local_image.jpg');">&nbsp;</a>
  109.         </div>
  110.         
  111. <!--Home Page - Panel-->
  112.         
  113.             <div data-role="panel" data-position-fixed="true" id="nav-panel" data-theme="b">
  114.             <ul data-role="listview" class="nav-search">
  115.                 <li data-icon="delete"><a href="#" data-rel="close">Close menu</a></li>
  116. <li><a href="#home">Home</a></li>
  117.                     <li><a href="#bestprices">Best Prices</a></li>
  118.                     <li><a href="#salesindex">Sales</a></li>
  119.                      <li><a href="#salesindex">Store Locator</a></li>
  120.                     <li><a href="#adviceindex">Advice</a></li>
  121.                   
  122.             </ul>
  123.         </div>
  124.         <!-- /panel -->
  125.         
  126. <!--Home Page - Content-->
  127.         
  128.         <div data-role="content" data-theme="a">
  129.         <div id="pageimage"><img src="img/banners/banner-name-buy.png" width="320"></div>
  130.                 
  131.                 <div id="content-window">  
  132.                 
  133.                  <h3>Welcome</h3>                     
  134.                 <ul data-role="listview" data-inset="true" data-theme="a"><!--Menu List/Start-->
  135.                
  136.                    
  137.                         <li><a href="#bestprices"><h3>Best Prices</h3></a></li>
  138.                 
  139.                         <li><a href="#salesindex" ><h3>Sales &amp; Promotions</h3></a></li>
  140.                 
  141.                         <li><a href="#locatorindex">
  142.                   <h3>Store Locator</h3></a></li>
  143.                         
  144.                         <li><a href="#about"><strong></strong><h3>App Information</h3></a></li>
  145.                     </ul><!--Menu List/end-->
  146.                                       <div class="ui-grid-a" data-theme="a">
  147.                         <div class="ui-block-a" data-theme="a">
  148.                         <a href="#" data-role="button" data-theme="a" onClick="window.open('http://www.facebook.com/pagename','_blank','location=no');return false;">Facebook</a></div>
  149.                         <div class="ui-block-b" data-theme="a">
  150.                         <a href="index.html" data-role="button" data-theme="a">Contact Me</a></div>
  151. </div><!-- /grid-a -->

  152. </div><!--End Content Div-->
  153.     
  154.     </div> <!-- content window div -->
  155. <!--Home Page - Footer-->
  156.        <div data-role="footer" data-position="fixed"><!--Start of below footer-->
  157.             <div data-role="navbar">
  158.                 <ul>
  159.                     <li><a href="#" class="ui-btn-active ui-state-persist" data-icon="custom">Home</a></li>
  160.                     <li><a href="#bestprices" data-icon="custom">Prices</a></li>
  161.                     <li><a href="#salesindex" data-icon="custom" >Sales</a></li>
  162.                     <li><a href="#adviceindex" data-icon="custom" >Advice</a></li>
  163.                 </ul> 
  164.             </div><!--End navbar -->
  165. </div><!--End Footer Div-->
  166.     
  167.     </div><!--End Home Page-->
  168.     
  169.   

  170. <div data-role="page" id="pagenamehere">
  171.    
  172.         <div data-role="header" data-position="fixed">
  173.                 <h2>Page Name</h2>
  174.                 <a href="#nav-panel" data-icon="bars" data-iconpos="notext">Menu</a>
  175.                 <a data-icon="info" data-rel="dialog" onClick="window.plugins.social.share('Message goes here', 'http://www.myurl.com.au/', 'www/image/local_image.jpg');">&nbsp;</a>
  176.         </div>
  177.         

  178.         
  179.             <div data-role="panel" data-position-fixed="true" id="nav-panel" data-theme="b">
  180.             <ul data-role="listview" class="nav-search">
  181.                 <li data-icon="delete"><a href="#" data-rel="close">Close menu</a></li>
  182. <li><a href="#home">Home</a></li>
  183.                     <li><a href="#bestprices">Best Prices</a></li>
  184.                     <li><a href="#salesindex">Sales</a></li>
  185.                      <li><a href="#salesindex">Store Locator</a></li>
  186.                     <li><a href="#adviceindex">Advice</a></li>
  187.                    
  188.             </ul>
  189.         </div>
  190.         <!-- /panel -->
  191.         
  192.         
  193.         <div data-role="content" data-theme="a">
  194.         <div id="pageimage"><img src="img/banners/banner-name.png" width="320"></div>
  195.                 
  196.                 <div id="content-window">  
  197.                 
  198.                    <div class="ui-grid-a" data-theme="a">
  199.                         <div class="ui-block-a" data-theme="a">
  200.                         <h4>Best Prices</h4>
  201.                         <p>Test</p>
  202.                         </div>
  203.                         <div class="ui-block-b" data-theme="a">
  204.                         <h4>Best Prices</h4>
  205.                         <p>Test</p>
  206.                         </div>
  207. </div><!-- /grid-a -->
  208.                     
  209.                 
  210.                 <div id="datacontent" data-role="content"></div><!-- /JSON Content Here -->
  211.     

  212. </div><!--End Content Div-->
  213.     
  214.     </div> <!-- content window div -->
  215.        <div data-role="footer" data-position="fixed"><!--Start of below footer-->
  216.             <div data-role="navbar">
  217.                 <ul>
  218.                     <li><a href="#home" data-icon="custom">Home</a></li>
  219.                     <li><a href="#bestprices" class="ui-btn-active ui-state-persist" data-icon="custom">Prices</a></li>
  220.                     <li><a href="#salesindex" data-icon="custom" >Sales</a></li>
  221.                     <li><a href="#adviceindex" data-icon="custom" >Advice</a></li>
  222.                 </ul> 
  223.             </div><!--End navbar -->
  224. </div><!--End Footer Div-->
  225.     
  226.     </div>
  227.     
  228. </body>
  229. </html>