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

Lal*_*ali 9 html javascript android jquery-mobile cordova

我正在制作一个phonegap/cordova项目.我使用命令行创建了一个骨架项目,因为指南建议制作一个新的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);
    }
};
Run Code Online (Sandbox Code Playgroud)

Lal*_*ali 3

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