conceptual problem or cache problem between pages

conceptual problem or cache problem between pages

Hi,

I am facing a cache problem in the  client side  authentication method.
I use a nice navigation panel on my webapp. It works well except when I need to log into my webapp.

This is my method :

5 files :
- index.html (or whatever.html) : index page including index.js and navpanel.js
- index.js (or whatever.js)  : ajax methods 
- auth.html : authentication page including auth.js
- auth.js : ajax method wich redirect you on index.html if authentication is true

- navpanel.js : ajax method wich display in a panel menu the links you need to use (it's not a really security issue, I check it on server side)

I don't really understand why, but i t seems that because navpanel is loaded everytime those functions are already in the DOM.  when I need to call renderPanel() after the authentication form process (wich works), I am redirected to index.html but my navigation panel is empty. I have to refresh my page and the navigation panel is displayed properly.

Could someone explain me why and how can I correct this ?
I tried to disable ajax-cache and others things but with no results...

Thx guys


This is my index.html page :
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta charset="utf-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1">
  6.     <title lang="en">Wiseflat - home</title>
  7.     <link rel="stylesheet" href="css/themes/default/jquery.mobile-1.3.2.min.css">
  8.     <link rel="stylesheet" href="_assets/css/jqm-demos.css">
  9.     <!--<link rel="stylesheet" href="_assets/css/google-fonts.css">-->
  10.     <link rel="shortcut icon" href="favicon.ico">
  11.     <script src="js/jquery.js"></script>
  12.     <script src="js/webx/jquery.validate.js"></script>
  13.     <script src="js/webx/additional-methods.js"></script>
  14.     <script src="js/jquery.mobile-1.3.2.min.js"></script>
  15.     <script src="js/jquery-lang.js" charset="utf-8" type="text/javascript"></script>
  16.     <script src="js/langpack/fr.js" charset="utf-8" type="text/javascript"></script>
  17.     <script type="text/javascript">
  18.         window.lang = new jquery_lang_js();
  19.         $().ready(function () {
  20.             window.lang.run();
  21.         });
  22.     </script>
  23. </head>
  24. <body>

  25. <div data-role="page" class="jqm-demos jqm-demos-index" id="index">

  26.     <div data-role="header" class="jqm-header">
  27.         <h1 class="jqm-logo"><a href="index.html" data-ajax="false" id="nav_getIndex"><img src="_assets/img/jquery-logo.png" alt="Wiseflat"></a></h1>
  28.         <a href="#nav-panel" class="jqm-navmenu-link" data-icon="bars" data-iconpos="notext" lang="en">Navigation</a>
  29.     </div><!-- /header -->

  30.     <div data-role="content" class="jqm-content">
  31.         <h4 lang="en">Weather</h4>
  32.         <div id="index_debug"></div>
  33.         <div class="jqm-list">
  34.             <ul data-role="listview" data-theme="d" data-inset="true" id="index_weather"></ul>
  35.         </div>
  36.         <h4 lang="en">Scenarios</h4>
  37.         <div class="jqm-list">
  38.             <ul data-role="listview" data-theme="d" data-inset="true" id="index_scenarios"></ul>
  39.         </div>
  40.         <!--<h4 lang="en">Last events</h4>
  41.         <div class="jqm-list">
  42.             <ul data-role="listview" data-theme="d" data-inset="true" id="index_events"></ul>
  43.         </div>-->


  44.     </div><!-- /content -->

  45.     <div data-role="footer" class="jqm-footer">
  46.         <p class="jqm-version"></p>
  47.         <p>Copyright 2012 Wiseflat</p>
  48.     </div><!-- /footer -->

  49.     <div data-role="panel" class="jqm-nav-panel jqm-navmenu-panel" data-position-fixed="true" data-position="left" data-display="reveal" data-theme="c" id="nav-panel">
  50.         <ul data-role="listview" id="nav_panel_list" data-theme="d" data-divider-theme="d" data-icon="false" data-global-nav="demos" class="jqm-list"></ul>
  51.     </div><!-- /panel -->

  52.     <script src="js/webx/navpanel.js"></script>
  53.     <script src="js/webx/index.js"></script>
  54. </div>
  55. </body>
  56. </html>

This is an exemple of my ajax POST/PUT/GET methods from index.js :

  1. function findAll() {
  2.     $.ajax({
  3.         type: 'GET',
  4.         url: rootURL + '/all',
  5.         dataType: "json",
  6.         success: function(data){
  7.             jsondata = data;
  8.             if(jsondata.auth == false){
  9.                 $.mobile.changePage("auth.html");
  10.             }else {
  11.                 renderList(jsondata);
  12.                 renderPanel(jsondata);
  13.             }
  14.         },
  15.         error: function(jqXHR, textStatus, errorThrown){
  16.             debug('error', 'List unsuccessfully fetched');
  17.         }
  18.     });
  19. }
if data.auth ==false, I redirect to auth.html

This is the main part of my auth.html page :

  1. <div data-role="page" class="jqm-demos jqm-demos-index" id="locations">

  2.     <div data-role="content" class="jqm-content">

  3.     <div class="jqm-list">
  4.         <form id="auth_form">
  5.             <p>Login: <input type="text" name="auth_login" id="auth_login" /></p>
  6.             <p>Password: <input type="password" name="auth_password" id="auth_password" /></p>
  7.             <div id="demo_container" >
  8.                 <button data-mini="true" id="auth_btnLogin" data-mini="true" lang="en">Login</button>
  9.             </div>
  10.         </form>
  11.     </div><!-- /content -->

  12.     <br />
  13.     <div id="auth_debug"></div>
  14.     <br />

  15.     <div data-role="footer" class="jqm-footer"></div><!-- /footer -->

  16.     <script src="js/webx/auth.js"></script>
  17. </div><!-- /page -->
This is my auth.js file :

  1. var rootURL = "/webx/api";
  2. var debug_mode=0;

  3. $(document).on('click', '#auth_btnLogin', function(e){
  4.     e.stopImmediatePropagation();
  5.     $("#auth_form").validate({
  6.         errorLabelContainer: "#auth_debug",
  7.         rules: {
  8.             auth_login: {
  9.                 required: true,
  10.                 textOnly: true
  11.             }
  12.         },
  13.         submitHandler: function(form){
  14.             auth();
  15.             return false;
  16.         }
  17.     });
  18. });

  19. function auth() {
  20.     //debug('debug', 'auth()');
  21.     $.ajax({
  22.         type: 'POST',
  23.         cache: false,
  24.         contentType: 'application/json',
  25.         url: rootURL + '/login',
  26.         dataType: "json",
  27.         data: formToJSON(),
  28.         success: function(data, textStatus, jqXHR){
  29.             if(data.auth == false){
  30.                 $.mobile.changePage("auth.html");
  31.             }
  32.             else {
  33.                 $.mobile.changePage("index.html", {reloadPage:false,changeHash:false});
  34.             }
  35.         },
  36.         error: function(jqXHR, textStatus, errorThrown){
  37.             //debug('error', 'Category unsuccessfully added');
  38.         }
  39.     });
  40. }


  41. function formToJSON() {
  42.     return JSON.stringify({
  43.         "login": $('#auth_login').val(),
  44.         "password": $('#auth_password').val()
  45.     });
  46. }

And the last one : navpanel.js : renderpanel()

  1. function renderPanel(data) {
  2.     $('#nav_panel_list li').remove();
  3.     $('#nav_panel_list').append('<li data-role="list-divider" lang="en">Navigation</li>');
  4.     $('#nav_panel_list').append('<li><a href="indexWeather.html" data-ajax="false" id="nav_getIndexWeather" lang="en">Weather</a></li>');
  5.     $('#nav_panel_list').append('<li><a href="indexDevices.html" data-ajax="false" id="nav_getIndexDevices" lang="en">Devices</a></li>');
  6.     $('#nav_panel_list').append('<li><a href="indexAreas.html" data-ajax="false" id="nav_getIndexAreas" lang="en">Areas</a></li>');
  7.     $('#nav_panel_list').append('<li><a href="indexScenarios.html" data-ajax="false" id="nav_getIndexScenarios" lang="en">Scenarios</a></li>');
  8.     $('#nav_panel_list').append('<li><a href="indexEvents.html" data-ajax="false" id="nav_getIndexEvents" lang="en">Events</a></li>');
  9.     if(data.group > 0){
  10.         $('#nav_panel_list').append('<li><a href="#" data-ajax="false" id="nav_getLogout" lang="en">Logout</a></li>');
  11.     }
  12.     else {
  13.         $('#nav_panel_list').append('<li data-role="list-divider" lang="en">Logged as '+data.login+'</li>');
  14.         $('#nav_panel_list').append('<li><a href="auth.html" data-ajax="false" id="nav_getLogin" lang="en">Sign in</a></li>');
  15.     }
  16.     if(data.group > 1){
  17.         $('#nav_panel_list').append('<li data-role="list-divider" lang="en">Settings</li>');
  18.         $('#nav_panel_list').append('<li><a href="users.html" data-ajax="false" id="nav_getUsers" lang="en">Users</a></li>');
  19.         $('#nav_panel_list').append('<li><a href="areas.html" data-ajax="false" id="nav_getAreas" lang="en">Areas</a></li>');
  20.         $('#nav_panel_list').append('<li><a href="locations.html" data-ajax="false" id="nav_getLocation" lang="en">Locations</a></li>');
  21.         $('#nav_panel_list').append('<li><a href="categories.html" data-ajax="false" id="nav_getCategories" lang="en">Categories</a></li>');
  22.         $('#nav_panel_list').append('<li><a href="devices.html" data-ajax="false" id="nav_getDevices" lang="en">Devices</a></li>');
  23.         $('#nav_panel_list').append('<li><a href="scenarios.html" data-ajax="false" id="nav_getScenarios" lang="en">Scenarios</a></li>');
  24.         $('#nav_panel_list').append('<li><a href="network.html" data-ajax="false" id="nav_getNetwork" lang="en">Network</a></li>');

  25.         $('#nav_panel_list').append('<li data-role="list-divider" lang="en">Plugins</li>');
  26.         $('#nav_panel_list').append('<li><a href="sms.html" data-ajax="false" id="nav_getSms" lang="en">Sms</a></li>');
  27.         $('#nav_panel_list').append('<li><a href="scripts.html" data-ajax="false" id="nav_getPlugins" lang="en">Scripts</a></li>');
  28.         $('#nav_panel_list').append('<li><a href="services.html" data-ajax="false" id="nav_getServices">Services</a></li>');
  29.     }
  30.     $('#nav_panel_list').append('<li data-role="list-divider" lang="en">About</li>');
  31.     $('#nav_panel_list').append('<li><a href="about.html" data-ajax="false" id="index_about" id="nav_getAbout" lang="en">me</a></li>');
  32.     $('#nav_panel_list').listview('refresh');
  33.     $('#nav_panel_list').trigger('create');

  34.     window.lang.change(data.lang);
  35.     window.lang.run(data.lang);
  36. }