【问题标题】:Do I need to add app.initialize() in all my html files in a cordova/phonegap project我需要在cordova/phonegap项目的所有html文件中添加app.initialize()吗
【发布时间】:2013-05-02 19:35:10
【问题描述】:

我正在制作一个 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 调用新页面(也就是说,只需像打开新页面一样打开新的 .html),那么是的,全部调用。
  • 读一本书,它说当检测到一个页面的外部链接时,托管在同一服务器/域上,jQueryMobile 将使用 Ajax 调用该页面。我已经尝试过了,其他页面运行顺利,我只需要确定它是否应该这样工作。

标签: javascript android html cordova jquery-mobile


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    • 2017-01-30
    • 2017-12-07
    • 2022-08-20
    • 2016-12-02
    相关资源
    最近更新 更多