changePage was firing more than once because hash listening was not disabled long enough to prevent listening of the resulting async hashchange event after setting location.hash. This change makes urlHistory.listeningEnabled act as an internal toggle (as it used to), so it is only meant for ignoring a single hashchange event. A future change might be appropriate to change its name to something more like "ignoreNextHashChange".

Unit test included.

Fixes #930
This commit is contained in:
scottjehl 2011-01-31 23:47:01 -05:00
parent 3162428558
commit 35b1df81e7
2 changed files with 26 additions and 5 deletions

View file

@ -351,11 +351,10 @@
function loadComplete(){
if( changeHash !== false && url ){
if( !back ){
urlHistory.listeningEnabled = false;
}
//disable hash listening temporarily
urlHistory.listeningEnabled = false;
//update hash and history
path.set( url );
urlHistory.listeningEnabled = true;
}
//add page to history stack if it's not back or forward
@ -679,7 +678,12 @@
//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; }
){
if( !urlHistory.listeningEnabled ){
urlHistory.listeningEnabled = true;
}
return;
}
//if to is defined, load it
if ( to ){

View file

@ -40,6 +40,8 @@
test( "path.get method is working properly", function(){
same($.mobile.path.get(), window.location.hash, "get method returns location.hash");
same($.mobile.path.get( "#foo/bar/baz.html" ), "foo/bar/", "get method with hash arg returns path with no filename or hash prefix");
@ -170,4 +172,19 @@
testListening( $.mobile.hashListeningEnabled );
});
asynctest( "changepage will only run once when a new page is visited", function(){
var called = 0;
$.mobile.changePage = function(){
changePageFn( arguments );
called ++;
};
$('#foo a').click();
setTimeout(function(){
start();
ok(called == 1, "change page should be called once");
}, 500);
});
})(jQuery);