JQM events and navigation

JQM events and navigation

I have sample site with three pages:

home.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="ISO-8859-1">
  5. <title>Home</title>
  6. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css" />
  7. <link rel="stylesheet" href="css/swipe.css"/>
  8. <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
  9. <script type="text/javascript">
  10. /*
  11. Here is where you would wire in the jqm mobileinit event such as:
  12. $( document ).on( "mobileinit" , function () {
  13. $.mobile.toolbar.prototype.options.addBackBtn = true;
  14. });
  15. */
  16. $( document ).on( "mobileinit" , function () {
  17. });
  18. </script>
  19. <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
  20. <!-- <script type="text/javascript" src="js/jquery.mobile.event.debugger.js"></script> -->
  21. <script type="text/javascript">
  22. var d=$(document);
  23. var specific=false;
  24. var pbc='pagecontainerbeforecreate';
  25. var pc='pagecontainercreate';
  26. var pbch='pagecontainerbeforechange'
  27. pbc='pagebeforecreate';
  28. pc='pagecreate';
  29. $('a').on('click',function(e){
  30. dumpEvent(e, 'clicked');
  31. });
  32. d.on(pbc, function(e, ui){
  33. dumpEvent(e, 'beforecreate', ui);
  34. }).on(pc, function(e, ui) {
  35. dumpEvent(e, 'create', ui)
  36. }).on(pbch, function(e, ui) {
  37. dumpEvent(e, 'beforechange', ui)
  38. });
  39. $(window).on('navigate', function(e, ui) {
  40. console.log('navigate=%O',ui);
  41. });
  42. d.on(pbc, '[data-url$="/next.html"]', function(e, ui){
  43. dumpEvent(e, 'next beforecreate', ui);
  44. }).on(pc, '[data-url$="/next.html"]', function(e, ui) {
  45. dumpEvent(e, 'next create', ui)
  46. }).on(pbch, '[data-url$="/next.html"]', function(e, ui) {
  47. dumpEvent(e, 'next beforechange', ui)
  48. });
  49. d.on(pbc, '[data-url$="/third.html"]', function(e, ui){
  50. dumpEvent(e, 'third beforecreate', ui);
  51. }).on(pc, '[data-url$="/third.html"]', function(e, ui) {
  52. dumpEvent(e, 'third create', ui)
  53. }).on(pbch, '[data-url$="/third.html"]', function(e, ui) {
  54. dumpEvent(e, 'third beforechange', ui)
  55. });
  56. function dumpEvent(e, msg, ui) {
  57. var prev=null;
  58. var to=null;
  59. if (ui) {
  60. if (ui.prevPage) prev=ui.prevPage[0];
  61. if (ui.toPage) to=ui.toPage[0];
  62. }
  63. console.log('%s fired on %O prev=%O to=%O%s', e.type, (e.target), prev, to, (msg?(" with message "+msg) :''));
  64. }
  65. $(document).ready(function(){
  66. console.log('document ready');
  67. })
  68. </script>
  69. </head>
  70. <body>
  71. <div data-role="page" id="home">
  72. <div data-role="header"><h3>Home</h3></div>
  73. <div role="main" class="ui-content">
  74. Goto the next <a href="next.html">page</a>
  75. </div>
  76. <div data-role="footer">Home</div>
  77. </div>
  78. </body>
  79. </html>

next.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="ISO-8859-1">
  5. <title>Next</title>
  6. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css" />
  7. <link rel="stylesheet" href="css/swipe.css"/>
  8. <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
  9. <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
  10. </head>
  11. <body>

  12. <div data-role="page" id="next">
  13. <div data-role="header"><h3>Next</h3></div>
  14. <div role="main" class="ui-content">
  15. <p>Rather simple page</p>
  16. <p>Goto the last <a href="third.html">page</a></p>
  17. </div>
  18. <div data-role="footer">Next</div>
  19. </div>
  20. <script type="text/javascript">
  21. $(document).ready(function(){
  22. })
  23. </script>
  24. </body>
  25. </html>

third.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="ISO-8859-1">
  5. <title>Third</title>
  6. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css" />
  7. <link rel="stylesheet" href="css/swipe.css"/>
  8. <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
  9. <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
  10. </head>
  11. <body>

  12. <div data-role="page" id="third">
  13. <div data-role="header"><h3>Third</h3></div>
  14. <div role="main" class="ui-content">
  15. <p>End of the line</p>
  16. </div>
  17. <div data-role="footer">Third</div>
  18. </div>
  19. <script type="text/javascript">
  20. $(document).ready(function(){
  21. })
  22. </script>
  23. </body>
  24. </html>
When home is displayed, i want to create a table and load it via AJAX.  Then clicking on 1 of the rows in the table will link me to next which will also display a table loaded by AJAX (related to the table row clicked in home page).  When I click a row in the next page it will link me to the third page which will load a table using AJAX.  You get the idea. All three pages are external pages.

I wanted to know how to use events to achieve this functionality.  In order to test this I marked up the three pages as above (just a simple link to the next page) and I ran this by going to the home page, then clicking to the next page, then the third and use the browser back arrow twice to take me back to the home.  Here is the console log (annotated with the words "mouse clicked" when I click the links:

  1. pagecontainerbeforechange fired on body.ui-mobile-viewport prev=null to=div#home with message beforechange
  2. pagecontainerbeforechange fired on body.ui-mobile-viewport prev=null to=div#home with message beforechange
  3. pagebeforecreate fired on div#home prev=null to=null with message beforecreate
  4. pagecreate fired on div#home.ui-page.ui-page-theme-a prev=null to=null with message create
  5. document ready
  6. mouse clicked
  7. pagecontainerbeforechange fired on body.ui-mobile-viewport.ui-overlay-a prev=div#home.ui-page.ui-page-theme-a.ui-page-active to=h with message beforechange
  8. pagebeforecreate fired on div#next prev=null to=null with message next beforecreate
  9. pagebeforecreate fired on div#next prev=null to=null with message beforecreate
  10. pagecreate fired on div#next.ui-page.ui-page-theme-a prev=null to=null with message next create
  11. pagecreate fired on div#next.ui-page.ui-page-theme-a prev=null to=null with message create
  12. pagecontainerbeforechange fired on body.ui-mobile-viewport.ui-overlay-a prev=div#home.ui-page.ui-page-theme-a.ui-page-active to=div#next.ui-page.ui-page-theme-a with message beforechange
  13. mouse clicked
  14. pagecontainerbeforechange fired on body.ui-mobile-viewport.ui-overlay-a prev=div#next.ui-page.ui-page-theme-a.ui-page-active to=h with message beforechange
  15. pagebeforecreate fired on div#third prev=null to=null with message third beforecreate
  16. pagebeforecreate fired on div#third prev=null to=null with message beforecreate
  17. pagecreate fired on div#third.ui-page.ui-page-theme-a prev=null to=null with message third create
  18. pagecreate fired on div#third.ui-page.ui-page-theme-a prev=null to=null with message create
  19. pagecontainerbeforechange fired on body.ui-mobile-viewport.ui-overlay-a prev=div#next.ui-page.ui-page-theme-a.ui-page-active to=div#third.ui-page.ui-page-theme-a with message beforechange
  20. navigate=Object
  21. pagecontainerbeforechange fired on body.ui-mobile-viewport.ui-overlay-a prev=div#third.ui-page.ui-page-theme-a.ui-page-active to=h with message beforechange
  22. pagebeforecreate fired on div#next prev=null to=null with message next beforecreate
  23. pagebeforecreate fired on div#next prev=null to=null with message beforecreate
  24. pagecreate fired on div#next.ui-page.ui-page-theme-a prev=null to=null with message next create
  25. pagecreate fired on div#next.ui-page.ui-page-theme-a prev=null to=null with message create
  26. pagecontainerbeforechange fired on body.ui-mobile-viewport.ui-overlay-a prev=div#third.ui-page.ui-page-theme-a.ui-page-active to=div#next.ui-page.ui-page-theme-a with message beforechange
  27. navigate=Object
  28. pagecontainerbeforechange fired on body.ui-mobile-viewport.ui-overlay-a prev=div#next.ui-page.ui-page-theme-a.ui-page-active to=h with message beforechange
  29. pagecontainerbeforechange fired on body.ui-mobile-viewport.ui-overlay-a prev=div#next.ui-page.ui-page-theme-a.ui-page-active to=div#home.ui-page.ui-page-theme-a with message beforechange
You will see that I created two handlers for each of the following events:

  1. pagebeforecreate
  2. pagecreate
  3. pagecontainerbeforechange
One handler does not have a selector as the second argument to the on method and the other one has a selector for either the next or the third page.

My observations:

The beforecreate (where I would create the table) and the create (where I would add bindings to redirect to the next page) seem to be called:

  1. before displaying the home page
  2. before displaying the next page
  3. before displaying the third page
  4. before navigating back to the next page
However, it is not called before navigating back to the home page.  This means I could not rebuild the home page table (or I do not know how to do this).  Here is my first question:


why are the pagebeforecreate and pagecreate not called when navigating back and how should I detect that ? 

The next observation is that pagecontainerbeforechange event never triggers the second handler to be called (the one with the selector as the second argument in the on method).  Also this event is also called twice.  Once with ui.toPage[0] pointing to an object called h and the second time with ui.toPage[0] pointing to the correct div.

Why does the pagecontainerbeforechange not get called with the second handler?  Why is it called twice and what is this h object? 

All these questions make me think that the whole events things is pretty buggy in JQM 1.4.5 or I do not understand what I am doing (which is possible definitely).

I would appreciate any help anyone can provide as I have been struggling with this for several days.