Help with navigation

Help with navigation

Ok I'm at my wits end and this is the last issue that needs to be resolved before we can roll out the initial version of our dedicated mobile app. I have a page in our mobile app (courses.html) that internally transitions between several jQM "pages" all the while programmatically setting lists with data. Flows like this:

courses.html
 -> List of terms (#terms)
 -> List of disciplines (#disciplines)
 -> List of courses by discipline (#courses)
 -> List of classes by course (#classes)
 -> Class Info (#classinfo)

The first five jQM pages display lists. Terms displays a list that is populated by a SOAP Web Service call. The user clicks a term, the termid is stored and persisted using sessionStorage, and then the user transitions to #disciplines which is a static HTML list. The user selects a discipline, another AJAX call is made to a SOAP Web Service passing in the termid which is stored in sessionStorage along with the disciplineid which was selected from the discipline list. The user is transitioned to the #courses page and the list is updated. This goes on all the way to the end point which is class info. 

I can progress all the way through the navigational flow with no problem all the while seeing appropriate data. However the problem occurs when I click the back button. As soon as I click back and then select a new option from the previous list, the list on the new page is doubled up...or more specifically the list is appended to include the old options for the previous discipline/course along with the new options (newly selected item). I've tried $('#list_coursesbycourseid').html = ''; and nothing seems to clear the list out. I suspect I'm not handling or using the correct events but I'm not entirely sure what's going on. Here's piece of code as an example. WS calls have been omitted:

  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></title>
  7. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css">
  8. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  9. <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
  10. <script>
  11. $(document).ready(function () {
  12.     // Load Current Terms
  13.     getCurrentTerms();
  14.     // HANDLER: Handles term click
  15.     $('#list_terms').on('click', 'a', function () {
  16. // Store the selected TermID
  17.         localStorage.termid = $(this).attr('id');
  18. // Change the page to show all disciplines
  19. $.mobile.changePage($('#disciplines'), {
  20.             transition: "slide",
  21.             reverse: false
  22.         });
  23.     });
  24. // HANDLER: Handles discipline click
  25.     $('#list_disciplines').on('click', 'a', function () {
  26. // Store the selected discipline
  27.         localStorage.disciplineid = $(this).attr('id');
  28. // Call the web service, passing in the selected term and selected discipline
  29.         getCoursesByTermAndDiscipline(localStorage.termid, localStorage.disciplineid);
  30. $.mobile.changePage($('#courses'), {
  31. transition: "slide",
  32. reverse: false
  33. });
  34.     });

  35. // HANDLER: Handles course click
  36.     $('#list_courses').on('click', 'a', function () {
  37. // Store the selected discipline
  38.         localStorage.courseid = $(this).attr('id');
  39. // Call the web service, passing in the selected term and selected discipline
  40.         getCoursesByTermAndCourseId(localStorage.termid, localStorage.courseid);

  41. $.mobile.changePage($('#coursesbycourseid'), {
  42. transition: "slide",
  43. reverse: false
  44. });
  45.     });

  46. });

  47. function getCoursesByTermAndCourseId(termid, courseid) {
  48.     $.ajax({
  49.         type: 'post',
  50.         data: { "termid": termid, "courseid": courseid },
  51.         url: '<WEB SERVICE CALL>',
  52.         beforeSend: function () { $.mobile.loading('show') },
  53.         timeout: 10000,
  54.         error: function (xhr, status, error) { alert('Error: ' + xhr.status + ' - ' + error); },
  55.         dataType: 'xml',
  56.         success: function (response) {
  57.             $(response).find('channel').children().each(function () {
  58.                 $.mobile.loading('hide');
  59.                 var elm = $(this);
  60.                 var courseid = elm.find('courseid').text();
  61.                 var course = elm.find('course').text();
  62. var classnumber = elm.find('classnumber').text();
  63.                 var campuslocation = elm.find('room').text();
  64.                 var classstartdate = elm.find('classstartdate').text();
  65.                 var classenddate = elm.find('classenddate').text();
  66.                 var instructor = elm.find('instructor').text();
  67. $('#list_coursesbycourseid').html = '';
  68.                 $('#list_coursesbycourseid').append('<li><a href="#" id="' + classnumber + '"><h3>' + course + '</h3><p>Class Number: ' + classnumber + '<br>Location: ' + campuslocation + '<br>Dates: ' + classstartdate + ' - ' + classenddate + '<br>Instructor: ' + instructor + '</p></a></li>');
  69.                 $('#list_coursesbycourseid').listview('refresh');
  70.             });
  71.         },
  72.         failure: function (msg) { alert(msg); }
  73.     });
  74. }

  75. function getCoursesByTermAndDiscipline(termid, disciplineid) {
  76.     $.ajax({
  77.         type: 'post',
  78.         data: { "termid": termid, "disciplineid": disciplineid },
  79.         url: '<WEB SERVICE CALL>',
  80.         beforeSend: function () { $.mobile.loading('show') },
  81.         timeout: 10000,
  82.         error: function (xhr, status, error) { alert('Error: ' + xhr.status + ' - ' + error); },
  83.         dataType: 'xml',
  84.         success: function (response) {
  85.             $(response).find('channel').children().each(function () {
  86.                 $.mobile.loading('hide');
  87.                 var elm = $(this);
  88.                 var courseid = elm.find('courseid').text();
  89.                 var course = elm.find('course').text();
  90. $('#list_courses').html = '';
  91.                 $('#list_courses').append('<li><a href="#" id="' + courseid + '"><h3>' + course + '</h3></a></li>');
  92.                 $('#list_courses').listview('refresh');
  93.             });
  94.         },
  95.         failure: function (msg) { alert(msg); }
  96.     });
  97. }

  98. function getCurrentTerms() {
  99.     $.ajax({
  100.         type: 'post',
  101.         data: '{}',
  102.         url: '<WEB SERVICE CALL>',
  103.         beforeSend: function () { $.mobile.loading('show') },
  104.         timeout: 10000,
  105.         error: function (xhr, status, error) {alert('Error: ' + xhr.status + ' - ' + error);},
  106.         dataType: 'xml',
  107.         success: function (response) {
  108.             $.mobile.loading('hide');
  109.             $(response).find('channel').children().each(function () {
  110.                 var elm = $(this);
  111.                 var termid = elm.find('termid').text();
  112.                 var term = elm.find('term').text();
  113.                 $('#list_terms').append('<li><a href="#" id="' + termid + '"><h3>' + term + '</h3></a></li>');
  114.                 $('#list_terms').listview('refresh');
  115.             });
  116.         },
  117.         failure: function (msg) {alert(msg);}
  118.     });
  119. }
  120. </script>
  121. </head>

  122. <body>
  123. <div data-role="page" id="terms">
  124. <header>
  125. <a href="index.html"><img src="/mobile/images/header.png" width="310" height="85"></a>
  126. </header>
  127.     
  128.     <div data-role="header">
  129.     <a href="index.html" data-role="button" data-mini="true" data-icon="back">Back</a>
  130. <h4>Select Term</h4>
  131. </div>
  132.     
  133. <section data-role="content">
  134. <div class="content-primary">
  135.             <ul data-role="listview" id="list_terms"></ul>
  136.         </div>
  137. </section>
  138.     <div data-role="footer"></div>
  139. <footer data-role="navbar">
  140.     <ul>
  141.   <li><a href="http://m.facebook.com/miracostacc"><img src="/images/icon_facebook.gif" width="24" height="24"></a></li>      
  142.             <li><a href="http://m.flickr.com/photos/miracosta_college/sets/"><img src="/images/icon_flickr.gif" width="24" height="24"></a></li>
  143.             <li><a href="https://mobile.twitter.com/miracosta"><img src="/images/icon_twitter.gif" width="24" height="24"></a></li>
  144.             <li><a href="http://m.youtube.com/user/miracostacc"><img src="/images/icon_youtube.gif" width="24" height="24"></a></li>
  145.             <li><a href="/mobile/moreoptions.html" data-rel="dialog"><img src="images/icon_more.png" width="24" height="24"></a></li>
  146.         </ul>
  147. </footer>
  148. </div>

  149. <div data-role="page" id="disciplines">
  150. <header>
  151. <a href="index.html"><img src="/mobile/images/header.png" width="310" height="85"></a>
  152. </header>
  153.     
  154.     <div data-role="header">
  155.     <a href="#terms" data-role="button" data-mini="true" data-icon="back">Back</a>
  156. <h4>Disciplines</h4>
  157. </div>
  158.     
  159. <section data-role="content">
  160. <div class="content-primary">
  161.             <ul data-role="listview" id="list_disciplines" data-autodividers="true">
  162.                 <li><a href="#" id="ACCT"><h3>Accounting</h3></a></li>
  163.                 <li><a href="#" id="ADM"><h3>Administration of Justice</h3></a></li>
  164.                 <li><a href="#" id="ACE"><h3>American College English</h3></a></li>
  165. <li><a href="#" id="ANTH"><h3>Anthropology</h3></a></li>
  166. <li><a href="#" id="ARCH"><h3>Architecture</h3></a></li>
  167. <li><a href="#" id="ART"><h3>Art</h3></a></li>
  168. <li><a href="#" id="ASIA"><h3>Asian Studies</h3></a></li>
  169. <li><a href="#" id="ASTR"><h3>Astronomy</h3></a></li>
  170. <li><a href="#" id="ATHL"><h3>Athletics</h3></a></li>
  171. <li><a href="#" id="AUTO"><h3>Automotive Technology</h3></a></li>
  172. <li><a href="#" id="BIO"><h3>Biological Sciences</h3></a></li>
  173. <li><a href="#" id="BTEC"><h3>Biotechnology</h3></a></li>
  174. <li><a href="#" id="BUS"><h3>Business Administration</h3></a></li>
  175. <li><a href="#" id="BOT"><h3>Business Office Technology</h3></a></li>
  176. <li><a href="#" id="CRLP"><h3>Career and Life Planning</h3></a></li>
  177. <li><a href="#" id="CHEM"><h3>Chemistry</h3></a></li>
  178. <li><a href="#" id="CHLD"><h3>Child Development</h3></a></li>
  179. <li><a href="#" id="CISGT"><h3>CIS GT</h3></a></li>
  180. <li><a href="#" id="COMM"><h3>Communication</h3></a></li>
  181. <li><a href="#" id="CIS"><h3>Computer &amp; Information Science</h3></a></li>
  182. <li><a href="#" id="CS"><h3>Computer Science</h3></a></li>
  183. <li><a href="#" id="CSIT"><h3>Computer Studies &amp; Info Tech</h3></a></li>
  184. <li><a href="#" id="COUN"><h3>Counseling</h3></a></li>
  185. <li><a href="#" id="DNCE"><h3>Dance</h3></a></li>
  186. <li><a href="#" id="DESN"><h3>Design</h3></a></li>
  187. <li><a href="#" id="DRAF"><h3>Design Drafting Technology</h3></a></li>
  188. <li><a href="#" id="DRAM"><h3>Dramatic Arts</h3></a></li>
  189. <li><a href="#" id="EART"><h3>Earth Sciences</h3></a></li>
  190. <li><a href="#" id="ECON"><h3>Economics</h3></a></li>
  191. <li><a href="#" id="EDUC"><h3>Education</h3></a></li>
  192. <li><a href="#" id="ETEC"><h3>Energy Technology</h3></a></li>
  193. <li><a href="#" id="ENGR"><h3>Engineering</h3></a></li>
  194. <li><a href="#" id="ENGL"><h3>English</h3></a></li>
  195. <li><a href="#" id="ESL"><h3>English as a Second Language</h3></a></li>
  196. <li><a href="#" id="FILM"><h3>Film</h3></a></li>
  197. <li><a href="#" id="FREN"><h3>French</h3></a></li>
  198. <li><a href="#" id="GEOG"><h3>Geography</h3></a></li>
  199. <li><a href="#" id="GEOL"><h3>Geology</h3></a></li>
  200. <li><a href="#" id="GRMN"><h3>German</h3></a></li>
  201. <li><a href="#" id="GERO"><h3>Gerontology</h3></a></li>
  202. <li><a href="#" id="HEAL"><h3>Health Education</h3></a></li>
  203. <li><a href="#" id="HIST"><h3>History</h3></a></li>
  204. <li><a href="#" id="HORT"><h3>Horticulture</h3></a></li>
  205. <li><a href="#" id="HOSP"><h3>Hospitality</h3></a></li>
  206. <li><a href="#" id="HUMN"><h3>Humanities</h3></a></li>
  207. <li><a href="#" id="INTR"><h3>Interdisciplinary Studies</h3></a></li>
  208. <li><a href="#" id="ITAL"><h3>Italian</h3></a></li>
  209. <li><a href="#" id="JAPN"><h3>Japanese</h3></a></li>
  210. <li><a href="#" id="KINE"><h3>Kinesiology</h3></a></li>
  211. <li><a href="#" id="LRNS"><h3>Learning Skills</h3></a></li>
  212. <li><a href="#" id="LIBR"><h3>Library</h3></a></li>
  213. <li><a href="#" id="LING"><h3>Linguistics</h3></a></li>
  214. <li><a href="#" id="LIT"><h3>Literature</h3></a></li>
  215. <li><a href="#" id="MATH"><h3>Mathematics</h3></a></li>
  216. <li><a href="#" id="MAT"><h3>Media Arts and Technologies</h3></a></li>
  217. <li><a href="#" id="MAP"><h3>Medical Admin Professional</h3></a></li>
  218. <li><a href="#" id="MUS"><h3>Music</h3></a></li>
  219. <li><a href="#" id="MTEC"><h3>Music Technology</h3></a></li>
  220. <li><a href="#" id="NURS"><h3>Nursing</h3></a></li>
  221. <li><a href="#" id="NUTR"><h3>Nutrition</h3></a></li>
  222. <li><a href="#" id="OCEA"><h3>Oceanography</h3></a></li>
  223. <li><a href="#" id="PHAR"><h3>Pharmacology</h3></a></li>
  224. <li><a href="#" id="PHIL"><h3>Philosophy &amp; Religious Studies</h3></a></li>
  225. <li><a href="#" id="PHLB"><h3>Phlebotomy</h3></a></li>
  226. <li><a href="#" id="PHSE"><h3>Physical Education</h3></a></li>
  227. <li><a href="#" id="PHSN"><h3>Physical Science</h3></a></li>
  228. <li><a href="#" id="PHTH"><h3>Physical Therapy</h3></a></li>
  229. <li><a href="#" id="PHYS"><h3>Physics</h3></a></li>
  230. <li><a href="#" id="PLSC"><h3>Political Science</h3></a></li>
  231. <li><a href="#" id="PSYC"><h3>Psychology</h3></a></li>
  232. <li><a href="#" id="READ"><h3>Reading</h3></a></li>
  233. <li><a href="#" id="REAL"><h3>Real Estate</h3></a></li>
  234. <li><a href="#" id="SOC"><h3>Sociology</h3></a></li>
  235. <li><a href="#" id="SPAN"><h3>Spanish</h3></a></li>
  236. <li><a href="#" id="SURG"><h3>Surgical Technology</h3></a></li>
  237. <li><a href="#" id="TA"><h3>Theatre Arts</h3></a></li>
  238. <li><a href="#" id="WKEX"><h3>Work Experience Education</h3></a></li>
  239.             </ul>
  240.         </div>
  241. </section>
  242.     <div data-role="footer"></div>
  243. <footer data-role="navbar">
  244.     <ul>
  245.   <li><a href="http://m.facebook.com/miracostacc"><img src="/images/icon_facebook.gif" width="24" height="24"></a></li>      
  246.             <li><a href="http://m.flickr.com/photos/miracosta_college/sets/"><img src="/images/icon_flickr.gif" width="24" height="24"></a></li>
  247.             <li><a href="https://mobile.twitter.com/miracosta"><img src="/images/icon_twitter.gif" width="24" height="24"></a></li>
  248.             <li><a href="http://m.youtube.com/user/miracostacc"><img src="/images/icon_youtube.gif" width="24" height="24"></a></li>
  249.             <li><a href="/mobile/moreoptions.html" data-rel="dialog"><img src="images/icon_more.png" width="24" height="24"></a></li>
  250.         </ul>
  251. </footer>
  252. </div>

  253. <div data-role="page" id="courses">
  254. <header>
  255. <a href="index.html"><img src="/mobile/images/header.png" width="310" height="85"></a>
  256. </header>
  257.     
  258.     <div data-role="header">
  259.     <a href="#disciplines" data-role="button" data-mini="true" data-icon="back">Back</a>
  260. <h4>Courses</h4>
  261. </div>
  262.     
  263. <section data-role="content">
  264. <div class="content-primary">
  265.             <ul data-role="listview" id="list_courses"></ul>
  266.         </div>
  267. </section>
  268.     <div data-role="footer"></div>
  269. <footer data-role="navbar">
  270.     <ul>
  271.   <li><a href="http://m.facebook.com/miracostacc"><img src="/images/icon_facebook.gif" width="24" height="24"></a></li>      
  272.             <li><a href="http://m.flickr.com/photos/miracosta_college/sets/"><img src="/images/icon_flickr.gif" width="24" height="24"></a></li>
  273.             <li><a href="https://mobile.twitter.com/miracosta"><img src="/images/icon_twitter.gif" width="24" height="24"></a></li>
  274.             <li><a href="http://m.youtube.com/user/miracostacc"><img src="/images/icon_youtube.gif" width="24" height="24"></a></li>
  275.             <li><a href="/mobile/moreoptions.html" data-rel="dialog"><img src="images/icon_more.png" width="24" height="24"></a></li>
  276.         </ul>
  277. </footer>
  278. </div>

  279. </body>
  280. </html>