Phonegap 3 - 如何调用Javascript函数

时间:2014-04-15 13:59:08

标签: javascript cordova

我正在使用phonegap 3而我无法调用我的功能,它无法正常工作!

这是我的代码,它是创建联系人的基本示例,但我不会创建一堆按钮来执行不同的操作,如创建,删除列表和类似的东西,但我无法接受它。执行的唯一代码位于“OnDeviceReady”函数的内部。

我如何调用我的函数以及如何调用?因为基本的Javascript不会运行。谢谢!

的index.html

<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/index.css" />
        <title>PILLEpalle!</title>
    </head>
    <body>
        <div class="app">
            <h1>PhoneGap</h1>
            <div id="deviceready" class="blink">
                <p class="event listening">Connecting to Device</p>
                <p class="event received">Device is Ready</p>
            </div>
            <div id="contactcreated" class="blink">
                <p class="event listening">Creating Contact</p>
                <p class="event received">Contact created</p>
            </div>
        </div>
        <script type="text/javascript" src="phonegap.js"></script>
        <script type="text/javascript" src="js/index.js"></script>
        <script type="text/javascript">
            app.initialize();
        </script>
    </body>
</html>

我的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 explicity call 'app.receivedEvent(...);'
    onDeviceReady: function() {
        app.receivedEvent('deviceready');
        var myContact = navigator.contacts.create({"displayName": "Test User"});
        myContact.note = "This contact has a note.";
        console.log("The contact, " + myContact.displayName + ", note: " + myContact.note);
        app.receivedEvent('contactcreated');
        myContact.save(app.onContactSaveSuccess,app.onContactSaveError);
    },
    // 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);
    },
    onContactSaveSuccess: function(contact) {
        window.alert("Save Success");
    },
    onContactSaveError: function(contact) {
        window.alert("Save Failed");
    },
};

1 个答案:

答案 0 :(得分:1)

Seens就像你需要在POO上阅读:http://en.wikipedia.org/wiki/Prototype-based_programming

基本上,您在var app范围内创建的函数如下所示:app.myFunction();

如果您希望在发生设备事件时调用,则需要将其添加为设备事件的侦听器。请查看文档:{​​{3}}