Slippery Rock University | Dr. Deborah Whitfield | Go Browns! |
(Project File) Structure
Good list of commands – all of which are actually command line arguments to the phonegap command:
$ phonegap platform add android
platform | add, remove, list, update |
plugin | search, add, remove, list |
build | prepare, then compile |
emulate | |
serve |
<button id="myBtn" type="button">Click me</button> <script> function dostuff() { … } document.getElementById("myBtn").addEventListener("click", doStuff); </script>
Phonegap's Javascript code
Within the head section of the html, you will always need:<meta charset="utf-8"> <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">Within the body, you will always need:
<script type="text/javascript" src="cordova.js"></script>You will also always need to add the event listener either directly in the body or by including a .js that does so:
<script type="text/javascript" > document.addEventListener('deviceready', letsGO, false); function letsGO() { // startup Code here } </script>
Another way the inclusion of deviceready can be done is via the index.js code that is created for new projects using Phonegap desktop.
Open up the js folder and find index.js
Note the code that declares the object app. it contains functions
initialize, bindEvents, onDeviceReady, and received Event:
var app = { initialize: function() { this.bindEvents(); }, // 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) { console.log('Received Event: ' + id); } };
<div class="app">
<div id="deviceready">
<script type="text/javascript"> app.initialize(); </script>
Let's try some coding
Add a function to index.js called testit() that:The Desktop version takes care of the commands. PhoneGap Build does even more… You just upload the individual (custom) assets and it does the rest.