pagebeforeshow event not fired after call to changepage

pagebeforeshow event not fired after call to changepage

Hi there,

I'm just starting out in jQM development and have hit a bit of a brick wall.

In short, I have a javascript file and two pages. In the main page (index.html) I'm populating a listview dynamically, and registering the tap event for each and every item for this listview. The on tap event, in return, calls the changepage method to an external page (details.html). This works fine 100% of the time.

In the javascript file, I'm registering for events on pagebeforeshow for the details.html page. This works okay first time out, but any subsequent calls are not triggering the pagebeforeshow event at all. Having had a closer look i can see that the pagechange is being called, pagebeforechange event is being fired okay, but the pagebeforeshow is only fired for that particular item only (untill a complete refresh).

I have set up a sample for you to be able to look at. I would seriously appreciate any feedback given. For all I know - I may be using the wrong events at all.

Thanks,

Matt

Index.html
  1. <!DOCTYPE HTML>
  2. <html >
  3.    <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  5. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
  6. <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  7. <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
  8. <script src="jquery.custom.js" type="text/javascript"></script>
  9. </head>
  10. <body>
  11. <!-- All Items Search -->
  12. <div id="idxPage" data-role="page" style="height:50px;" data-title="Main Menu">
  13. <div data-role="header" class="sixteen columns">
  14. <a href="index.html" data-icon="home" data-direction="reverse">Home</a>
  15. <h1>
  16. <a href="index.html" data-icon="home" data-direction="reverse"> Test </a>
  17. </h1>
  18. </div>

  19. <div data-role="content" style="text-align:center;">
  20. <ul id="ListItems" data-role="listview" data-inset="true" data-filter="true">
  21. </ul>
  22. </div><!-- /content -->
  23. <footer>
  24. <div data-role="footer" data-id="connfooter" class="ui-footer ui-bar-a" role="contentinfo"> 
  25. <h4 style="text-align: left;" class="ui-title" tabindex="0" role="heading" aria-level="1">

  26. </h4>
  27. </div>
  28. </footer>
  29. </div>
  30. </body>
  31.  </html>
 details.html
  1. <!DOCTYPE HTML>
  2. <html >
  3.    <head>
  4. <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  5. <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
  6. <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  7. <script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
  8. <script src="jquery.custom.js" type="text/javascript"></script>
  9. </head>
  10. <body>
  11. <!-- All Items Search -->
  12. <div id="detailsPage" data-role="page" style="height:50px;" data-title="Main Menu">
  13. <div data-role="header" class="sixteen columns">
  14. <a href="index.html" data-icon="home" data-direction="reverse">Home</a>
  15. <h1>
  16. <a href="index.html" data-icon="home" data-direction="reverse"> Test </a>
  17. </h1>
  18. </div>

  19. <div data-role="content" style="text-align:center;">
  20. <b>Page placeholder</b>
  21. </div><!-- /content -->
  22. <footer>
  23. <div data-role="footer" data-id="connfooter" class="ui-footer ui-bar-a" role="contentinfo"> 
  24. <h4 style="text-align: left;" class="ui-title" tabindex="0" role="heading" aria-level="1">

  25. </h4>
  26. </div>
  27. </footer>
  28. </div>
  29. </body>
  30.  </html>
 jquery.custom.js (JS Library)
  1. $(document).on("pagebeforechange", function (event, data) {
  2.     alert('changing page...');
  3. });

  4. $(document).on('pageinit', function () {
  5. $("#detailsPage").off();
  6. $("#detailsPage").on("pagebeforeshow", function(event){
  7. alert('about to show page...');
  8. });

  9.     $("#ListItems").off();
  10.     $("#ListItems").on("listviewbeforefilter", function (e, data) {
  11.         var $ul = $(this),
  12. $input = $(data.input),
  13. value = $input.val(),
  14. html = "";
  15.         $ul.html("");

  16.         if (value && value.length > 2) {
  17.             $ul.html("<li><div class='ui-loader'><span class='ui-icon ui-icon-loading'></span></div></li>");
  18.             $ul.listview("refresh");

  19.             var max = 200;
  20.             var limit = 0;

  21. var itemslist = [
  22. {"id":1, "desc":"item1"},
  23. {"id":2, "desc":"item2"},
  24. {"id":3, "desc":"testitm1"},
  25. {"id":4, "desc":"testitm2"}
  26. ];
  27.             $.each(itemslist, function (i, val) {

  28.                 if (limit < max) {
  29.                     if (val.desc.toString().toLowerCase().indexOf(value.toLowerCase()) != -1) {
  30.                         $ul.append('<li id="' + val.id + '" ><a style="text-align:left" data-icon="arrow-r" data-iconpos="right" >' + val.desc + '</a></li>');

  31.                         $('#' + val.id).off();
  32.                         $('#' + val.id).on('tap', function (event) {
  33.                             var elementId = $(this).attr('id');

  34.                             $.mobile.changePage("details.html?Id="+elementId, { data: { "Id": elementId} });
  35.                         });

  36.                         limit++;
  37.                     }
  38.                 }

  39.             });

  40.             $ul.listview("refresh");
  41.             $ul.trigger("updatelayout");

  42.         }
  43.     });

  44. });