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:
- <!DOCTYPE html>
-
- <html>
-
- <head>
-
- <title>TuBa GameS</title>
-
- <meta name="viewport" content="width=device-width, initial-scale=1">
-
- <!-- CSS stylesheets -->
-
- <!-- Font Awesome CSS -->
- <link rel="stylesheet" type="text/css" href="font-awesome/css/font-awesome.min.css" />
-
- <!-- jQuery Mobile CSS -->
- <link rel="stylesheet" type="text/css" href="css/jquerymobile/jquerymobile.css" />
-
- <!-- RequireJS, load main script -->
- <script data-main="js/app/index.js" src="js/require.js"></script>
-
- </head>
-
- <body>
-
- <!-- ================== [ Page: #splash ] ================== -->
-
- <div data-role="page" id="splash">
-
- <div data-role="header" data-position="fixed">
- <h2>TuBa GameS</h2>
- </div>
-
- <div data-role="content">
-
- <ul data-role="listview">
-
- <li><a href="#userpass">TEST Page in DOM: #userpass</a></li>
-
- </ul>
-
- </div> <!-- END: content -->
-
- </div> <!-- END: page #splash -->
-
- <!-- ================== [ Page: #userpass ] ================== -->
-
- <div data-role="page" id="userpass">
-
- <div data-role="header" data-position="fixed">
- <h2>TuBa GameS</h2>
- </div>
-
- <div data-role="content">
-
- <p>Page: index.html#userpass</p>
-
- </div> <!-- END: content -->
-
- </div> <!-- END: page #userpass -->
-
- </body>
-
- </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:
- define ( ['jquery', 'jquerymobile'], // Modules required
-
- function ($) {
-
- /* We add the debug button to header (left side) of every JQM page
- * BEFORE being enhaced
- */
- console.log("Number of JQM pages in DOM: " + $("[data-role='page']").length);
-
- $("[data-role='page']").each ( function (index) {
-
- $(this).on( "pagebeforecreate", function( event, ui ) {
-
- var pageid = $(this).attr("id");
-
- console.debug("TuBa Engine JS [debug]: JQM page id=[" + pageid + "] is going to be enhaced!");
-
- }); // END: pagebeforecreate event
-
- }); // END: JQMpages.each
-
- }); // 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!