javascript - jQuery Mobile 应用程序的起点

标签 javascript jquery-mobile

如果这是一个“菜鸟”问题,我很抱歉,但是 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/

相关文章:

javascript - 在 .ready 之前加载 <div> 中的 HTML

javascript - 如何用鼠标滚动显示一个div在另一个div后面

javascript - 间歇性发布两次..使用IE

jquery - 展开可折叠时向下滑动内容

javascript - 使用 Rails 加载页面时的 jQuery Mobile 弹出窗口

jquery - Phonegap(+ jquery mobile)应用程序在浏览器上运行良好,但在设备上运行不佳

javascript - 在 Angular 中动态创建范围变量和 ng-model

关于 .push() 的 JavaScript

Javascript async parallel - 如何返回最终结果/回调?

c# - 我可以使用什么技术为我的 C# 应用程序编写一个 View ?