jQuery Mobile v1.4 , we are February 2014.
So, I read here (gihub) that we should do an if for the computed pagecontainer events to assume that the page is loaded.
Here is a small script trying to understand the intended behavior of the new pageContainer widget and its use.
A simple login page as such, pre-program the welcome page, and then switch to the welcome page for a successful login. There are several JS scripts on the welcome page to animate the page, it should only be run if the page is visible. How do we do this?
Here are the results that I got when investigating pagecontainer events through the console. My goal here is to find a way to find that my welcome (any page is actually) page active / visible.
I used this format as a query for understanding:
$( 'body' ).on( 'pagecontainerbeforeload', function( event, ui ) { console.log("beforeload"); console.log(event); console.log(ui); });
So, a new start, when I load the page and JQM for the first time (i.e. /Users/login )
Events can be used:
- pagecontainercreate => empty ui
- PCbeforetransition => ui.toPage useful
- PCshow => only gets ui.prevPage (which is empty in this case)
- PCtransition => ui.toPage useful
Now they always start, even if you have disabled transition effects ( See api )
Ok, then I want to programmatically load the page (first get it), I: (I want /Users/welcome )
$("body").pagecontainer("load", "/Users/welcome");
I fire this event (the page is not yet visible):
- PCbeforeload => I get a URL that I could use to identify the page.
- PCload => almost the same data as PCbeforeload
Ok, now I go to my page: (before /Users/welcome )
$("body").pagecontainer("change", "/Users/welcome");
I fire these events:
- PChide => ui.nextPage is the same as ui.toPage ...
- PCbeforetransition => ui.toPage useful
- PCshow => only gives ui.prevPage
- PCtransition => ui.toPage is present as expected
Ok, now I'm sure that the only pagecontainer event that I want to use to make sure this page is loaded is pagecontainertransition . Here is what I implemented on every page that should run JS:
Set Page Container ID (PHP)
<div data-role="page" data-theme='a' id="<?php echo $this->id_url()?>">
... at the end of the page (JS)
$( 'body' ).on( 'pagecontainertransition', function( event, ui ) { if(ui.toPage[0] == $('#'+id_url )[0] ) { functionToLaunchWhenPageShowUp(); } } );
Now, as you can see, I mean ui.toPage 1st child [0] , to compare it with $('.selector') 1st child [0] . Is this the right way to do this? I mean, the intended path using the new API. Thank you for sharing your knowledge;)