简体   繁体   中英

Do I need to add app.initialize() in all my html files in a cordova/phonegap project

I am making a phonegap/cordova project. I created a skeleton project using command line, as the guide suggests to making a new android/phonegap project.

In the index.html file created there is a piece of code app.initialize() , and the code it comes from a file called index.js.

My question is, do I have to have this piece of code in all my html files, since i will be using jQueryMobile to do the front-end, I might need to have several html files.

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);
    }
};

Since all pages are called through Ajax calls, in theory you don't need to add that line in all your pages. But in some cases you might want to add it, for example if there might be a chance that the particular page might not be called from an ajax call, or a user for some strange reason lands on that page, instead of your index page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM