需要解释 Cordova 'deviceready' 事件的工作原理

Need explanation about how Cordova 'deviceready' event works

我正在学习在 Cordova PhoneGap 中创建应用程序,我对这个 'deviceready' 事件的使用感到困惑。它应该是特定于 Cordova API 的事件,但在 Hello World 示例中,与它相关的所有内容都在 index.js 文件中定义。

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

.html 文件中有此脚本标记

<script src="cordova.js"></script>

所有这些设备就绪代码对我来说都像是不必要的代码。 请有人向我解释为什么需要这个 'deviceready' 事件,以及它与 Cordova API

的确切联系

deviceready事件是important.When你运行应用程序第一次加载Cordova API's,cordova pluginetc.When本次加载完成它会触发 deviceready event.It 就像 HTML 文件的 onload 事件。所以,你应该在 deviceready event.And cordova.js 之后或之后做你的工作,用原生平台初始化 cordova API。cordova.js 将在你构建时自动生成您的应用程序。<script src="cordova.js"></script> 是对 cordova.js file.You 的引用,可以在此处 link.

获取 deviceready 事件的详细信息