javascript - 我是否需要在 cordova/phonegap 项目的所有 html 文件中添加 app.initialize()

标签 javascript android html cordova jquery-mobile

我正在制作一个 phonegap/cordova 项目。我使用命令行创建了一个框架项目,作为 guide建议制作一个新的 android/phonegap 项目。

在创建的 index.html 文件中有一段代码 app.initialize(),它来自一个名为 index.js 的文件。

我的问题是,我的所有 html 文件中都必须有这段代码吗,因为我将使用 jQueryMobile 来做前端,我可能需要有几个 html 文件。

var app = {
    // Application Constructor
    initialize: function() {
        this.bindEvents();
    },
    // Bind Event Listeners
    //
    // Bind any events that are required on startup. Common events are:
    // 'load', 'deviceready', 'offline', and 'online'.
    bindEvents: function() {
        document.addEventListener('deviceready', this.onDeviceReady, false);
    },
    // deviceready Event Handler
    //
    // The scope of 'this' is the event. In order to call the 'receivedEvent'
    // function, we must explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
    },
    // Update DOM on a Received Event
    receivedEvent: function(id) {
        var parentElement = document.getElementById(id);
        var listeningElement = parentElement.querySelector('.listening');
        var receivedElement = parentElement.querySelector('.received');

        listeningElement.setAttribute('style', 'display:none;');
        receivedElement.setAttribute('style', 'display:block;');

        console.log('Received Event: ' + id);
    }
};

最佳答案

由于所有页面都是通过 Ajax 调用调用的,理论上您不需要在所有页面中添加该行。但在某些情况下,您可能想要添加它,例如,如果特定页面可能无法从 ajax 调用中调用,或者用户出于某种奇怪的原因登陆该页面,而不是您的索引页面。

关于javascript - 我是否需要在 cordova/phonegap 项目的所有 html 文件中添加 app.initialize(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16442922/

相关文章:

javascript - 使用 jQuery 解析 XML 并填充文本字段

android - 当数据是字符串而不是对象时,如何在 android 中使用解析相同的 json 响应?

android - 在Android中,一个activity可以有多少个线程?

javascript - 如何使用 Javascript 获取整页高度?

javascript - mocha-phantomjs 的 mocha init 超时

android - 每次我们想在应用程序中更改字体时,我们是否必须复制 Font TTF

javascript - 弹出窗口未显示在 Chrome 扩展程序中,但单击图标时会触发所有事件

javascript - 将事件绑定(bind)到两个函数并在一个函数内取消绑定(bind)时

javascript - 用不同的值替换多个不同的字符javascript

javascript - 获取数据集以使用数据表 ajax