如果这是一个“菜鸟”问题,我很抱歉,但是 jQuery 移动应用程序的起点在哪里?类似于在 Android 中启动 Activity 时调用的 onCreate()
方法。例如,如果我有一个条件,我想在其他任何事情发生之前检查(比如我想在家里显示或隐藏一个按钮如果条件为真或假,则进行筛选)。
if (localStorage.registered) {
//show button A
} else {
//show button B
}
我应该将执行此操作的逻辑放在哪里以确保它是第一个完成的事情?
最佳答案
在 jQuery Mobile 初始化期间触发的第一个点是 mobileinit 事件:
$( document ).on( "mobileinit", function() {
// We want popups to cover the page behind them with a dark background
$.mobile.popup.prototype.options.overlayTheme = "b";
// Set a namespace for jQuery Mobile data attributes
$.mobile.ns = "jqm-";
});
此事件在 jQuery Mobile 完成加载后但在开始增强起始页之前触发。因此,此事件的处理程序有机会在影响库的行为之前修改 jQuery Mobile 的全局配置选项和所有小部件的默认选项值。
此时基本上您需要立即做的所有事情。
之前这样做没有意义,因为 jQuery Mobile 甚至还没有初始化。
最后一件事,这个事件必须在 jQuery Mobile 初始化之前初始化,像这样(或者它想要工作):
<script src="//code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
$( document ).on( "mobileinit", function() {
// We want popups to cover the page behind them with a dark background
$.mobile.popup.prototype.options.overlayTheme = "b";
// Set a namespace for jQuery Mobile data attributes
$.mobile.ns = "jqm-";
});
</script>
<script src="//code.jquery.com/mobile/1.4.2/jquery.mobile-1.4.2.min.js"></script>
如果您正在创建一个 Phonegap 应用程序,并且您还想包含 Phonegap (Cordopva),那么请这样做:
var deviceReadyDeferred = $.Deferred();
var jqmReadyDeferred = $.Deferred();
document.addEventListener("deviceReady", deviceReady, false);
function deviceReady() {
deviceReadyDeferred.resolve();
}
$(document).one("mobileinit", function () {
jqmReadyDeferred.resolve();
});
$.when(deviceReadyDeferred, jqmReadyDeferred).then(doWhenBothFrameworksLoaded);
function doWhenBothFrameworksLoaded() {
}
关于javascript - jQuery Mobile 应用程序的起点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23759578/