Added ability to control if $.mobile will automatically load first page/pages on dom ready using $.mobile.autoInitialize.

This commit is contained in:
unknown 2011-02-01 23:28:40 -06:00 committed by John Bender
parent 675deb1282
commit 9caaa9cfde
2 changed files with 49 additions and 27 deletions

View file

@ -57,6 +57,9 @@
gradeA: function(){
return $.support.mediaquery;
},
//automatically initialize first pages or not.
autoInitialize: true,
//TODO might be useful upstream in jquery itself ?
keyCode: {
@ -167,37 +170,44 @@
setTimeout(function() {
$.event.special.scrollstart.enabled = true;
}, 150 );
},
// find and enhance the pages in the dom and transition to the first page.
initializePage: function(){
//find present pages
var $pages = $( "[data-role='page']" );
//add dialogs, set data-url attrs
$pages.add( "[data-role='dialog']" ).each(function(){
$(this).attr( "data-url", $(this).attr( "id" ));
});
//define first page in dom case one backs out to the directory root (not always the first page visited, but defined as fallback)
$.mobile.firstPage = $pages.first();
//define page container
$.mobile.pageContainer = $pages.first().parent().addClass( "ui-mobile-viewport" );
//cue page loading message
$.mobile.pageLoading();
// if hashchange listening is disabled or there's no hash deeplink, change to the first page in the DOM
if( !$.mobile.hashListeningEnabled || !$.mobile.path.stripHash( location.hash ) ){
$.mobile.changePage( $.mobile.firstPage, false, true, false, true );
}
// otherwise, trigger a hashchange to load a deeplink
else {
$window.trigger( "hashchange", [ true ] );
}
}
});
//dom-ready inits
$(function(){
//find present pages
var $pages = $( "[data-role='page']" );
//add dialogs, set data-url attrs
$pages.add( "[data-role='dialog']" ).each(function(){
$(this).attr( "data-url", $(this).attr( "id" ));
if($.mobile.autoInitialize){
$(function(){
$.mobile.initializePage();
});
//define first page in dom case one backs out to the directory root (not always the first page visited, but defined as fallback)
$.mobile.firstPage = $pages.first();
//define page container
$.mobile.pageContainer = $pages.first().parent().addClass( "ui-mobile-viewport" );
//cue page loading message
$.mobile.pageLoading();
// if hashchange listening is disabled or there's no hash deeplink, change to the first page in the DOM
if( !$.mobile.hashListeningEnabled || !$.mobile.path.stripHash( location.hash ) ){
$.mobile.changePage( $.mobile.firstPage, false, true, false, true );
}
// otherwise, trigger a hashchange to load a deeplink
else {
$window.trigger( "hashchange", [ true ] );
}
});
}
//window load event

View file

@ -34,7 +34,7 @@
test( "loading the core library triggers mobilinit on the document", function(){
expect( 1 );
$(window.document).bind('mobileinit', function(event){
$(window.document).one('mobileinit', function(event){
ok(true);
});
@ -145,5 +145,17 @@
stop();
$.testHelper.reloadLib(libName);
});
test( "auto initialization does not occur when set to false", function(){
$(window.document).one('mobileinit', function(event){
$.mobile.autoInitialize = false;
});
$.testHelper.reloadLib(libName);
ok($("html").hasClass("ui-mobile-rendering"), "Still in rendering state after library load.");
$.mobile.initializePage();
ok(!$("html").hasClass("ui-mobile-rendering"), "Rendered ok after call to initializePage");
});
});
})(jQuery);