[jQuery] Architecture for google Gears.
Hi
I'm doing some research implementing Google Gears.
To work with Gears it is necessary to know if the web server is accessible.
The solution I have seen so far, is to ping the server periodically, and
to set a boolean var "isOnline" accordingly.
Then when a request needs to be executed, the var is checked to know
if the request goes to the webserver of to gears.
I did not like this, because it is not robust. The server can fail just
after having been pinged, and receive a request because "isOnline" is
still set to true.
So I tried the following approach:
-intercept form submission
-submit the data with AJAX
---->if the server is down, pass the request data to gears
---->if the server response is ok, load that response
Well, this is not so easy to do.
I came up with that:
(function($){
$.fn.gearify = function(options){
var defaults = {};
var options = $.extend(defaults, options);
var submitToGears = function(a){
console.log("function submitToGears() : "+a);
if (submitToGears && window.google && google.gears){
var db =
google.gears.factory.create('beta.database');
}
}
return this.each(function(){
$(this).submit(function(){
$.ajax({type: 'post',
url: $(this).attr('action'),
data: {dummy: "bla", stuff: "blo" },
success: function(data){
if ($('h1',
data).html() == 'An Error Was Encountered') {
//DB is down
submitToGears('db error');
}
else {
//Server is up
console.log('NO need for gears');
document.write(data);
}
},
error: function(data){
submitToGears('error.
server down. '+data); // Server down
}
});
return false;
});
});
}
})(jQuery);
$('form').gearify();
ISSUES:
In this version of the code, the problem is to correctly overwrite the
page. It almost work, but document.write() produces javascript errors on
xhtml documents,
makes the page loading forever, and generate funnies "console is not
defined" messages in firebug.
Since the AJAX call is asynchronous, It seems that I can not kill the
submit event depending on the AJAX result.
Any ideas about this approach ? Is this architecture not doable ?
Thanks
-Olivier