fixed up things so that deviceready now uses DOMContentLoaded and if DCL fires before something is attached to deviceready the function will be called immediately

This commit is contained in:
Dave Johnson
2010-07-20 11:25:55 -07:00
parent 27c4de6aa0
commit f7a4a0aef8
6 changed files with 325 additions and 143 deletions

View File

@@ -0,0 +1,81 @@
PhoneGap.Channel = function(type)
{
this.type = type;
this.handlers = {};
this.guid = 0;
this.fired = false;
this.enabled = true;
};
PhoneGap.Channel.prototype.sub = function(f, c, g)
{
// need a function to call
if (f == null)
return;
var func = f;
if (typeof c == "object" && f instanceof Function)
func = PhoneGap.close(c, f);
g = g || func.observer_guid || f.observer_guid || this.guid++;
func.observer_guid = g;
f.observer_guid = g;
this.handlers[g] = func;
return g;
};
PhoneGap.Channel.prototype.sob = function(f, c)
{
var g = null;
var _this = this;
var m = function() {
f.apply(c || null, arguments);
_this.dub(g);
}
if (this.fired) {
if (typeof c == "object" && f instanceof Function)
f = PhoneGap.close(c, f);
f.apply(this, this.fireArgs);
} else {
g = this.sub(m);
}
return g;
};
PhoneGap.Channel.prototype.dub = function(g)
{
if (g instanceof Function)
g = g.observer_guid;
this.handlers[g] = null;
delete this.handlers[g];
};
PhoneGap.Channel.prototype.fire = function(e)
{
if (this.enabled)
{
var fail = false;
for (var item in this.handlers) {
var handler = this.handlers[item];
if (handler instanceof Function) {
var rv = (handler.apply(this, arguments)==false);
fail = fail || rv;
}
}
this.fired = true;
this.fireArgs = arguments;
return !fail;
}
return true;
};
PhoneGap.Channel.merge = function(h, e) {
var i = e.length;
var f = function() {
if (!(--i)) h();
}
for (var j=0; j<i; j++) {
(!e[j].fired?e[j].sob(f):i--);
}
if (!i) h();
}

View File

@@ -57,3 +57,20 @@ Device.prototype.exitApp = function()
PhoneGap.addConstructor(function() {
navigator.device = window.device = new Device();
});
PhoneGap.onDeviceReady = new PhoneGap.Channel();
PhoneGap.__document_addEventListener = document.addEventListener;
document.addEventListener = function(evt, handler, capture) {
if (evt.toLowerCase() == 'deviceready') {
PhoneGap.onDeviceReady.sob(handler);
} else {
PhoneGap.__document_addEventListener.call(document, evt, handler);
}
}
document.addEventListener('DOMContentLoaded', function() {
PhoneGap.onDeviceReady.fire();
})

View File

@@ -37,52 +37,21 @@ PhoneGap.addConstructor = function(func) {
}
};
(function()
{
var timer = setInterval(function()
{
var state = document.readyState;
if ( state == 'loaded' || state == 'complete' )
{
clearInterval(timer); // stop looking
// run our constructors list
while (PhoneGap._constructors.length > 0)
{
var constructor = PhoneGap._constructors.shift();
try
{
constructor();
}
catch(e)
{
if (typeof(debug['log']) == 'function')
{
debug.log("Failed to run constructor: " + debug.processMessage(e));
}
else
{
alert("Failed to run constructor: " + e.message);
}
}
document.addEventListener('DOMContentLoaded', function() {
// run our constructors list
while (PhoneGap._constructors.length > 0) {
var constructor = PhoneGap._constructors.shift();
try {
constructor();
} catch(e) {
if (typeof(debug['log']) == 'function') {
debug.log("Failed to run constructor: " + debug.processMessage(e));
} else {
alert("Failed to run constructor: " + e.message);
}
// Start listening for callbacks from Java
PhoneGap.JSCallback();
// all constructors run, now fire the deviceready event
var e = document.createEvent('Events');
e.initEvent('deviceready');
document.dispatchEvent(e);
// Fire the onresume event, since first one happens before we are inited
var e = document.createEvent('Events');
e.initEvent('onresume');
document.dispatchEvent(e);
}
}, 5);
})();
}
}
});
/**
* Execute a PhoneGap command in a queued fashion, to ensure commands do not