From 78041bc7c9c8e8e0d795d6200c2b04f775bd30a6 Mon Sep 17 00:00:00 2001 From: Kin Blas Date: Thu, 8 Sep 2011 09:21:31 -0700 Subject: [PATCH] Added path.isFirstPageUrl() which is now called from loadPage() when trying to determine if the URL being loaded refers to a page that is already in the DOM. This will prevent us from duplicating the first-page in the main application document. Also checking in the first example of how to use the pagebeforechange notification to allow for dynamically updating and re-using a page that is already in the DOM. --- .../dynamic-samples/sample-reuse-page.html | 198 ++++++++++++++++++ js/jquery.mobile.navigation.js | 26 +++ 2 files changed, 224 insertions(+) create mode 100644 docs/pages/dynamic-samples/sample-reuse-page.html diff --git a/docs/pages/dynamic-samples/sample-reuse-page.html b/docs/pages/dynamic-samples/sample-reuse-page.html new file mode 100644 index 00000000..851800cd --- /dev/null +++ b/docs/pages/dynamic-samples/sample-reuse-page.html @@ -0,0 +1,198 @@ + + + + + +changePage JSON Sample + + + + + + + +
+

Categories

+
+

Select a Category Below:

+ +
+
+
+

+
+
+ + diff --git a/js/jquery.mobile.navigation.js b/js/jquery.mobile.navigation.js index bce278f9..cf20a35c 100755 --- a/js/jquery.mobile.navigation.js +++ b/js/jquery.mobile.navigation.js @@ -216,6 +216,26 @@ return ( /^(:?\w+:)/ ).test( url ); }, + //check if the specified url refers to the first page in the main application document. + isFirstPageUrl: function( url ) { + // We only deal with absolute paths. + var u = path.parseUrl( path.makeUrlAbsolute( url, documentBase ) ), + + // Does the url have the same path as the document? + samePath = u.hrefNoHash === documentUrl.hrefNoHash || ( documentBaseDiffers && u.hrefNoHash === documentBase.hrefNoHash ), + + // Get the first page element. + fp = $.mobile.firstPage, + + // Get the id of the first page element if it has one. + fpId = fp && fp[0] ? fp[0].id : undefined; + + // The url refers to the first page if the path matches the document and + // it either has no hash value, or the hash is exactly equal to the id of the + // first page element. + return samePath && ( !u.hash || u.hash === "#" || ( fpId && u.hash.replace( /^#/, "" ) === fpId ) ); + }, + isEmbeddedPage: function( url ) { var u = path.parseUrl( url ); @@ -605,6 +625,12 @@ // Check to see if the page already exists in the DOM. page = settings.pageContainer.children( ":jqmData(url='" + dataUrl + "')" ); + // If we failed to find a page in the DOM, check the URL to see if it + // refers to the first page in the application. + if ( page.length === 0 && $.mobile.firstPage && path.isFirstPageUrl( absUrl ) ) { + page = $( $.mobile.firstPage ); + } + // Reset base to the default document base. if ( base ) { base.reset();