问题
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