Fix problem with deviceready being called before device properties are guaranteed to be set.

This commit is contained in:
Bryce Curtis 2010-09-20 15:39:54 -05:00
parent a59cad68e2
commit ace84227cc
2 changed files with 89 additions and 8 deletions

30
framework/assets/js/device.js Normal file → Executable file
View File

@ -12,22 +12,46 @@ function Device() {
this.phonegap = null; this.phonegap = null;
var me = this; var me = this;
PhoneGap.execAsync( this.getInfo(
function(info) { function(info) {
me.available = true; me.available = true;
me.platform = info.platform; me.platform = info.platform;
me.version = info.version; me.version = info.version;
me.uuid = info.uuid; me.uuid = info.uuid;
me.phonegap = info.phonegap; me.phonegap = info.phonegap;
PhoneGap.onPhoneGapInfoReady.fire();
}, },
function(e) { function(e) {
me.available = false; me.available = false;
console.log("Error initializing PhoneGap: " + e); console.log("Error initializing PhoneGap: " + e);
alert("Error initializing PhoneGap: "+e); alert("Error initializing PhoneGap: "+e);
}, });
"Device", "getDeviceInfo", []);
} }
/**
* Get device info
*
* @param {Function} successCallback The function to call when the heading data is available
* @param {Function} errorCallback The function to call when there is an error getting the heading data. (OPTIONAL)
*/
Device.prototype.getInfo = function(successCallback, errorCallback) {
// successCallback required
if (typeof successCallback != "function") {
console.log("Device Error: successCallback is not a function");
return;
}
// errorCallback optional
if (errorCallback && (typeof errorCallback != "function")) {
console.log("Device Error: errorCallback is not a function");
return;
}
// Get info
PhoneGap.execAsync(successCallback, errorCallback, "Device", "getDeviceInfo", []);
};
/* /*
* This is only for Android. * This is only for Android.
* *

View File

@ -1,4 +1,25 @@
/**
* The order of events during page load and PhoneGap startup is as follows:
*
* onDOMContentLoaded Internal event that is received when the web page is loaded and parsed.
* window.onload Browser's body onload event.
* onNativeReady Internal event that indicates the PhoneGap native side is ready.
* onPhoneGapInit Internal event that kicks off creatation all PhoneGap JavaScript objects (runs constructors)
* onPhoneGapReady Internal event fired when all PhoneGap JavaScript ojbects have been created
* onPhoneGapInfoReady Internal event fired when device properties are available
* onDeviceReady User event fired to indicate that PhoneGap is ready
* onResume User event fired to indicate a start/resume lifecycle event
*
* The only PhoneGap events that user code should register for are:
* onDeviceReady
* onResume
*
* Listeners can be registered as:
* document.addEventListener("deviceready", myDeviceReadyListener, false);
* document.addEventListener("resume", myResumeListener, false);
*/
if (typeof(DeviceInfo) != 'object') if (typeof(DeviceInfo) != 'object')
DeviceInfo = {}; DeviceInfo = {};
@ -124,7 +145,7 @@ PhoneGap.available = DeviceInfo.uuid != undefined;
* @param {Function} func The function callback you want run once PhoneGap is initialized * @param {Function} func The function callback you want run once PhoneGap is initialized
*/ */
PhoneGap.addConstructor = function(func) { PhoneGap.addConstructor = function(func) {
PhoneGap.onDeviceReady.subscribeOnce(function() { PhoneGap.onPhoneGapInit.subscribeOnce(function() {
try { try {
func(); func();
} catch(e) { } catch(e) {
@ -162,6 +183,23 @@ PhoneGap.onDOMContentLoaded = new PhoneGap.Channel('onDOMContentLoaded');
*/ */
PhoneGap.onNativeReady = new PhoneGap.Channel('onNativeReady'); PhoneGap.onNativeReady = new PhoneGap.Channel('onNativeReady');
/**
* onPhoneGapInit channel is fired when the web page is fully loaded and
* PhoneGap native code has been initialized.
*/
PhoneGap.onPhoneGapInit = new PhoneGap.Channel('onPhoneGapInit');
/**
* onPhoneGapReady channel is fired when the JS PhoneGap objects have been created.
*/
PhoneGap.onPhoneGapReady = new PhoneGap.Channel('onPhoneGapReady');
/**
* onPhoneGapInfoReady channel is fired when the PhoneGap device properties
* has been set.
*/
PhoneGap.onPhoneGapInfoReady = new PhoneGap.Channel('onPhoneGapInfoReady');
/** /**
* onResume channel is fired when the PhoneGap native code * onResume channel is fired when the PhoneGap native code
* resumes. * resumes.
@ -180,22 +218,41 @@ PhoneGap.onPause = new PhoneGap.Channel('onPause');
if (typeof _nativeReady !== 'undefined') { PhoneGap.onNativeReady.fire(); } if (typeof _nativeReady !== 'undefined') { PhoneGap.onNativeReady.fire(); }
/** /**
* onDeviceReady is fired only after both onDOMContentLoaded and * onDeviceReady is fired only after all PhoneGap objects are created and
* onNativeReady have fired. * the device properties are set.
*/ */
PhoneGap.onDeviceReady = new PhoneGap.Channel('onDeviceReady'); PhoneGap.onDeviceReady = new PhoneGap.Channel('onDeviceReady');
/**
* Start listening for XHR callbacks onDeviceReady
*/
PhoneGap.onDeviceReady.subscribeOnce(function() { PhoneGap.onDeviceReady.subscribeOnce(function() {
PhoneGap.JSCallback(); PhoneGap.JSCallback();
}); });
/**
* Create all PhoneGap objects once page has fully loaded and native side is ready.
*/
PhoneGap.Channel.join(function() {
// Run PhoneGap constructors
PhoneGap.onPhoneGapInit.fire();
// Fire event to notify that all objects are created
PhoneGap.onPhoneGapReady.fire();
}, [ PhoneGap.onDOMContentLoaded, PhoneGap.onNativeReady ]);
/**
* Fire onDeviceReady event once all constructors have run and PhoneGap info has been
* received from native side.
*/
PhoneGap.Channel.join(function() { PhoneGap.Channel.join(function() {
PhoneGap.onDeviceReady.fire(); PhoneGap.onDeviceReady.fire();
// Fire the onresume event, since first one happens before JavaScript is loaded // Fire the onresume event, since first one happens before JavaScript is loaded
PhoneGap.onResume.fire(); PhoneGap.onResume.fire();
}, [ PhoneGap.onDOMContentLoaded, PhoneGap.onNativeReady ]); }, [ PhoneGap.onPhoneGapReady, PhoneGap.onPhoneGapInfoReady]);
// Listen for DOMContentLoaded and notify our channel subscribers // Listen for DOMContentLoaded and notify our channel subscribers
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {