JQM events and navigation
I have sample site with three pages:
home.html
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="ISO-8859-1">
- <title>Home</title>
- <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css" />
- <link rel="stylesheet" href="css/swipe.css"/>
- <script src="http://code.jquery.com/jquery-1.11.1.js"></script>
- <script type="text/javascript">
- /*
- Here is where you would wire in the jqm mobileinit event such as:
- $( document ).on( "mobileinit" , function () {
- $.mobile.toolbar.prototype.options.addBackBtn = true;
- });
- */
- $( document ).on( "mobileinit" , function () {
- });
- </script>
- <script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
- <!-- <script type="text/javascript" src="js/jquery.mobile.event.debugger.js"></script> -->
- <script type="text/javascript">
- var d=$(document);
- var specific=false;
- var pbc='pagecontainerbeforecreate';
- var pc='pagecontainercreate';
- var pbch='pagecontainerbeforechange'
- pbc='pagebeforecreate';
- pc='pagecreate';
- $('a').on('click',function(e){
- dumpEvent(e, 'clicked');
- });
- d.on(pbc, function(e, ui){
- dumpEvent(e, 'beforecreate', ui);
- }).on(pc, function(e, ui) {
- dumpEvent(e, 'create', ui)
- }).on(pbch, function(e, ui) {
- dumpEvent(e, 'beforechange', ui)
- });
- $(window).on('navigate', function(e, ui) {
- console.log('navigate=%O',ui);
- });
- d.on(pbc, '[data-url$="/next.html"]', function(e, ui){
- dumpEvent(e, 'next beforecreate', ui);
- }).on(pc, '[data-url$="/next.html"]', function(e, ui) {
- dumpEvent(e, 'next create', ui)
- }).on(pbch, '[data-url$="/next.html"]', function(e, ui) {
- dumpEvent(e, 'next beforechange', ui)
- });
- d.on(pbc, '[data-url$="/third.html"]', function(e, ui){
- dumpEvent(e, 'third beforecreate', ui);
- }).on(pc, '[data-url$="/third.html"]', function(e, ui) {
- dumpEvent(e, 'third create', ui)
- }).on(pbch, '[data-url$="/third.html"]', function(e, ui) {
- dumpEvent(e, 'third beforechange', ui)
- });
- function dumpEvent(e, msg, ui) {
- var prev=null;
- var to=null;
- if (ui) {
- if (ui.prevPage) prev=ui.prevPage[0];
- if (ui.toPage) to=ui.toPage[0];
- }
- console.log('%s fired on %O prev=%O to=%O%s', e.type, (e.target), prev, to, (msg?(" with message "+msg) :''));
- }
- $(document).ready(function(){
- console.log('document ready');
- })
- </script>
- </head>
- <body>
- <div data-role="page" id="home">
- <div data-role="header"><h3>Home</h3></div>
- <div role="main" class="ui-content">
- Goto the next <a href="next.html">page</a>
- </div>
- <div data-role="footer">Home</div>
- </div>
- </body>
- </html>
next.html
third.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:
- pagecontainerbeforechange fired on body.ui-mobile-viewport prev=null to=div#home with message beforechange
- pagecontainerbeforechange fired on body.ui-mobile-viewport prev=null to=div#home with message beforechange
- pagebeforecreate fired on div#home prev=null to=null with message beforecreate
- pagecreate fired on div#home.ui-page.ui-page-theme-a prev=null to=null with message create
- document ready
- mouse clicked
- 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
- pagebeforecreate fired on div#next prev=null to=null with message next beforecreate
- pagebeforecreate fired on div#next prev=null to=null with message beforecreate
- pagecreate fired on div#next.ui-page.ui-page-theme-a prev=null to=null with message next create
- pagecreate fired on div#next.ui-page.ui-page-theme-a prev=null to=null with message create
- 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
- mouse clicked
- 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
- pagebeforecreate fired on div#third prev=null to=null with message third beforecreate
- pagebeforecreate fired on div#third prev=null to=null with message beforecreate
- pagecreate fired on div#third.ui-page.ui-page-theme-a prev=null to=null with message third create
- pagecreate fired on div#third.ui-page.ui-page-theme-a prev=null to=null with message create
- 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
- navigate=Object
- 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
- pagebeforecreate fired on div#next prev=null to=null with message next beforecreate
- pagebeforecreate fired on div#next prev=null to=null with message beforecreate
- pagecreate fired on div#next.ui-page.ui-page-theme-a prev=null to=null with message next create
- pagecreate fired on div#next.ui-page.ui-page-theme-a prev=null to=null with message create
- 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
- navigate=Object
- 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
- 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:
- pagebeforecreate
- pagecreate
- 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:
- before displaying the home page
- before displaying the next page
- before displaying the third page
- 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.