Skip to content

Instantly share code, notes, and snippets.

@kerrishotts
Created October 15, 2016 20:29
Show Gist options
  • Save kerrishotts/4e22050e0f77821ad287b84d147365ad to your computer and use it in GitHub Desktop.
Save kerrishotts/4e22050e0f77821ad287b84d147365ad to your computer and use it in GitHub Desktop.
Battery Status example for phonegap forum post
function batteryStatus(z) {
alert ("level: " + z.level + "; isPlugged: " + z.isPlugged);
}
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');
window.addEventListener("batterystatus", batteryStatus, false);
},
// 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);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment