jQuery移动应用的起点



很抱歉,如果这是一个" noby "的问题,但是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() {
}