Reputation: 293
I'm using phonegap 3 and I'm unable to call my functions, it doesen't work !
This is my code it's a basic sample for create contacts, but I won't to create a bunch of buttons to do different things like create, delete list and stuff like this but I can't get with it. The only code executed is in the inside of "OnDeviceReady" function.
How can i call my functions and how ? Because the basic Javascript doesen't run. Thanks !
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>
My js file
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");
},
};
Upvotes: 1
Views: 3823
Reputation: 6997
Seens like you need some reading on POO: http://en.wikipedia.org/wiki/Prototype-based_programming
Basically, the functions you've created inside the var app scope you call like this: app.myFunction();
If you want then to be called when a device event occurs, then you need to add then as a listener to the device events. Take a look at the docs: http://docs.phonegap.com/en/3.4.0/cordova_events_events.md.html#Events
Upvotes: 1