jQuery Mobile + requirejs: pagebeforecreate event randomly firing/not firing

jQuery Mobile + requirejs: pagebeforecreate event randomly firing/not firing

Hi to all, I need some help!

I'm coding a debug library that will inject a button in every jQuery Mobile page header. I'm using one HTML file, multiple jQuery Mobile pages inside, like this:

  1. <!DOCTYPE html>

  2. <html>

  3. <head>

  4.     <title>TuBa GameS</title>
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <!-- CSS stylesheets -->
  7. <!-- Font Awesome CSS -->
  8. <link rel="stylesheet" type="text/css" href="font-awesome/css/font-awesome.min.css" />
  9. <!-- jQuery Mobile CSS -->
  10. <link rel="stylesheet" type="text/css" href="css/jquerymobile/jquerymobile.css" />
  11. <!-- RequireJS, load main script -->
  12. <script data-main="js/app/index.js" src="js/require.js"></script>
  13. </head>

  14. <body>

  15. <!-- ================== [ Page: #splash ] ================== -->

  16. <div data-role="page" id="splash">

  17.     <div data-role="header" data-position="fixed">
  18.    <h2>TuBa GameS</h2>
  19. </div>
  20. <div data-role="content">
  21.        <ul data-role="listview">
  22. <li><a href="#userpass">TEST Page in DOM: #userpass</a></li>
  23. </ul>
  24.     
  25. </div> <!-- END: content -->

  26. </div> <!-- END: page #splash -->

  27. <!-- ================== [ Page: #userpass ] ================== -->

  28. <div data-role="page" id="userpass">

  29.     <div data-role="header" data-position="fixed">
  30.    <h2>TuBa GameS</h2>
  31. </div>
  32. <div data-role="content">
  33.    <p>Page: index.html#userpass</p>
  34. </div> <!-- END: content -->

  35. </div> <!-- END: page #userpass -->

  36. </body>

  37. </html>

I'm using requirejs, and writing the "header button inject" code in jQuery Mobile's "pagebeforecreate", so I add the button BEFORE ENHACEMENT IN EVERY data-role="page" <div>

The problem is that the "pagebeforecreate" event is randomly firing: sometimes it doesn't fire at all, sometimes it's fired for only the "visible" page, sometimes for two pages... I supposse that is a problem related with requirejs and dependencies order.

This is the code of the debug requirejs module:

  1. define ( ['jquery', 'jquerymobile'], // Modules required

  2. function ($) {

  3.     /* We add the debug button to header (left side) of every JQM page
  4.      * BEFORE being enhaced
  5.      */
  6.     console.log("Number of JQM pages in DOM: " + $("[data-role='page']").length);
  7.     
  8.     $("[data-role='page']").each ( function (index) { 
  9.    $(this).on( "pagebeforecreate", function( event, ui ) {
  10.         
  11.        var pageid = $(this).attr("id");
  12.         
  13.        console.debug("TuBa Engine JS [debug]: JQM page id=[" + pageid + "] is going to be enhaced!");
  14.         
  15.    }); // END: pagebeforecreate event
  16. }); // END: JQMpages.each
  17.     
  18. }); // END: requireJS module

Sorry about my poor english, I'm spanish and I'm afraid that my english is not very good. I hope you can help me with this problem. Thanks in advance, guys! You are really awesome!