From 3162428558efd9960739ef8f00a7ead0dbf6d3ec Mon Sep 17 00:00:00 2001 From: scottjehl Date: Mon, 31 Jan 2011 23:25:36 -0500 Subject: [PATCH] added hashListeningEnabled global flag. This is meant as a global config option for end users to disable hashchange listening (as opposed to urlHistory.listeningEnabled, which is an internal toggle). Unit test included. Fixes #748 --- js/jquery.mobile.core.js | 5 ++++- js/jquery.mobile.navigation.js | 7 ++++--- tests/unit/navigation/navigation_core.js | 15 ++++++++++++--- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/js/jquery.mobile.core.js b/js/jquery.mobile.core.js index 8448f435..28c174ce 100644 --- a/js/jquery.mobile.core.js +++ b/js/jquery.mobile.core.js @@ -29,6 +29,9 @@ //automatically handle clicks and form submissions through Ajax, when same-domain ajaxEnabled: true, + + //automatically load and show pages based on location.hash + hashListeningEnabled: true, // TODO: deprecated - remove at 1.0 //automatically handle link clicks through Ajax, when possible @@ -187,7 +190,7 @@ $.mobile.pageLoading(); // if hashchange listening is disabled or there's no hash deeplink, change to the first page in the DOM - if( $.mobile.urlHistory.listeningEnabled == false || !$.mobile.path.stripHash( location.hash ) ){ + if( !$.mobile.hashListeningEnabled || !$.mobile.path.stripHash( location.hash ) ){ $.mobile.changePage( $.mobile.firstPage, false, true, false, true ); } // otherwise, trigger a hashchange to load a deeplink diff --git a/js/jquery.mobile.navigation.js b/js/jquery.mobile.navigation.js index 945f01e7..2649b087 100644 --- a/js/jquery.mobile.navigation.js +++ b/js/jquery.mobile.navigation.js @@ -117,7 +117,8 @@ urlHistory.stack = urlHistory.stack.slice( 0, urlHistory.activeIndex + 1 ); }, - //enable/disable hashchange event listener + //enable/disable hashchange event listener internally + //for use in toggling temporarily, rather than disabling globally //toggled internally when location.hash is updated to match the url of a successful page load listeningEnabled: true }, @@ -675,8 +676,8 @@ //transition is false if it's the first page, undefined otherwise (and may be overridden by default) transition = $.mobile.urlHistory.stack.length === 0 ? false : undefined; - //if listening is disabled, or it's a dialog hash - if( urlHistory.listeningEnabled == false || + //if listening is disabled (either globally or temporarily), or it's a dialog hash + if( !$.mobile.hashListeningEnabled || !urlHistory.listeningEnabled || urlHistory.stack.length > 1 && to.indexOf( dialogHashKey ) > -1 && !$.mobile.activePage.is( ".ui-dialog" ) ){ return; } diff --git a/tests/unit/navigation/navigation_core.js b/tests/unit/navigation/navigation_core.js index 3838a437..216536a9 100644 --- a/tests/unit/navigation/navigation_core.js +++ b/tests/unit/navigation/navigation_core.js @@ -147,8 +147,8 @@ }); //url listening - asyncTest( "ability to disable our hash change event listening", function(){ - $.mobile.urlHistory.listeningEnabled = false; + function testListening( prop ){ + prop = false; var stillListening = false; $(document).bind("pagebeforehide", function(){ stillListening = true; @@ -156,9 +156,18 @@ location.hash = "foozball"; setTimeout(function(){ start(); - ok( $.mobile.urlHistory.listeningEnabled == stillListening, "urlHistory.listeningEnabled = false disables default hashchange event handler"); + ok( prop == stillListening, "urlHistory.listeningEnabled = false disables default hashchange event handler"); location.hash = ""; + prop = true; }, 1000); + } + + asyncTest( "ability to disable our hash change event listening internally", function(){ + testListening( $.mobile.urlHistory.listeningEnabled ); + }); + + asyncTest( "ability to disable our hash change event listening globally", function(){ + testListening( $.mobile.hashListeningEnabled ); }); })(jQuery); \ No newline at end of file