From aac698520e27ecd8be81c31abf1417a9d20956cb Mon Sep 17 00:00:00 2001 From: toddparker Date: Thu, 28 Jul 2011 16:26:34 -0400 Subject: [PATCH] Added clearer messaging about dom ready and how to use pagecreate instead. --- docs/api/events.html | 53 +++++++++++++++++++++----------------------- 1 file changed, 25 insertions(+), 28 deletions(-) diff --git a/docs/api/events.html b/docs/api/events.html index 77b5b0e1..23df5cf1 100755 --- a/docs/api/events.html +++ b/docs/api/events.html @@ -25,6 +25,10 @@

jQuery Mobile offers several custom events that build upon native events to create useful hooks for development. Note that these events employ various touch, mouse, and window events, depending on event existence, so you can bind to them for use in both handheld and desktop environments. You can bind to these events like you would with other jQuery events, using live() or bind().

+
+

Important: Use pageCreate(), not $(document).ready()

+ The first thing you learn in jQuery is to call code inside the $(document).ready() function so everything will execute as soon as the DOM is loaded. However, in jQuery Mobile, Ajax is used to load the contents of each page into the DOM as you navigate, and the DOM ready handler only executes for the first page. To execute code whenever a new page is loaded and created, you can bind to the pagecreate event. This event is explained in detail at the bottom of this page.
+

Touch events

tap
@@ -76,8 +80,7 @@

Note that all four of these events expose a reference to either the next page (nextPage) or previous page (prevPage), depending on whether the page is being shown or hidden, and whether that next or previous page exists (the first ever page shown does not have a previous page to reference, but an empty jQuery object is provided just the same). You can access this reference via the second argument of a bound callback function. For example:

-
-		
+

 $('div').live('pageshow',function(event, ui){
   alert('This page was just hidden: '+ ui.prevPage);
 });
@@ -85,8 +88,7 @@ $('div').live('pageshow',function(event, ui){
 $('div').live('pagehide',function(event, ui){
   alert('This page was just shown: '+ ui.nextPage);
 });
-		
-		
+

Also, for these handlers to be invoked during the initial page load, you must bind them before jQuery Mobile executes. This can be done in the mobileinit handler, as described on the global config page.

Page initialization events

@@ -96,39 +98,34 @@ $('div').live('pagehide',function(event, ui){
pagebeforecreate
-
Triggered on the page being initialized, before initialization occurs.
-
pagecreate
-
Triggered on the page being initialized, after initialization occurs.
-
- - -
-		
+			
+

Triggered on the page being initialized, before initialization occurs.

+

 $('#aboutPage').live('pagebeforecreate',function(event){
   alert('This page was just inserted into the dom!');
 });
-
-$('#aboutPage').live('pagecreate',function(event){
-  alert('This page was just enhanced by jQuery Mobile!');
-});
-		
-		
+
+

Note that by binding to pagebeforecreate and returning false, you can prevent the page plugin from making its manipulations.

-

Note that by binding to pagebeforecreate and returning false, you can prevent the page plugin from making its manipulations.

- -
-		
+

 $('#aboutPage').live('pagebeforecreate',function(event){
   //run your own enhancement scripting here...
   return false;
 });
+
+ + +
pagecreate
+
+

Triggered on the page being initialized, after initialization occurs. We recommend binding to this event instead of DOM ready() because this will work regardless of whether the page is loaded directly or if the content is pulled into another page as part of the Ajax navigation system.

+

+$('#aboutPage').live('pagecreate',function(event){
+  alert('This page was just enhanced by jQuery Mobile!');
+});
+
+
+ -
-
- -
-

Note on Page IDs in Alpha 2 release (no longer an issue): In jQuery Mobile Alpha 2 and older, page elements utilized the ID attribute for storing the location from which they came. When you place an ID attribute on a page that is brought into jQuery Mobile's single-page environment through Ajax, jQuery Mobile wraps that page with a new "page" div element, preserving any CSS references to your ID. However, this means that your ID attribute is no longer on the "page" element, so you must keep this in mind when binding to page events (pagebeforecreate, pagecreate, etc). To avoid issues, try using a class if possible.

-

Animation Events