Is index.js in cordova project necessary?

无人久伴 提交于 2021-01-27 20:34:16

问题


Do I have to keep this file (www/js/index.js) or can I delete it and put my project files instead?

var app = {
    // Application Constructor
    initialize: function() {
        document.addEventListener('deviceready', this.onDeviceReady.bind(this), false);
    },

    // deviceready Event Handler
    //
    // Bind any cordova events here. Common events are:
    // 'pause', 'resume', etc.
    onDeviceReady: function() {
        this.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);
    }
};

app.initialize();

I used to work with Cocoon.io and there were no index.js file, i was handling deviceready event myself.


回答1:


As long as you are properly binding the deviceready event, you can replace or delete the index.js entirely. Make sure you also update your JS references from index.html accordinygly.

If nothing works when you test the app, it means you have broken the onDeviceReady handler.



来源:https://stackoverflow.com/questions/50368428/is-index-js-in-cordova-project-necessary

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!