扩展Phonegap的样板index.js

时间:2014-11-09 17:19:52

标签: javascript cordova

如果使用Phonegap 3.6 CLI创建应用程序,则会获得以下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;');

        console.log('Received Event: ' + id);
    }
};

虽然我知道JavaScript的基础知识,但我并不熟悉所使用的代码风格,所以如果有人会解释Phonegap文档中显示的不同部分和onDeviceReady: function()语法,我就会徘徊是不同的:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    // Now safe to use device APIs
}

我还想知道在哪里扩展示例代码以构建我自己的应用程序的最佳位置。我只是替换Update DOM on a Received Event部分,还是将我自己的.js文件包含在我的JavaScript自定义代码中?

0 个答案:

没有答案