AJAX - The Complete Reference

(avery) #1

PART III


Chapter 10: Web Services and Beyond 537


To see for yourself try our simple demo at http://ajaxref.com/ch10/helloworldoffline.html.
Considering our AjaxTCR library supports its own cache, it would seem likely that
being offline and accessing a cached response from memory would work, and it does.
However, our simple response cache doesn’t solve the offline problem, because what would
happen when you try to make a new request or post some data in offline mode? Errors, for
certain! Of course, if we give the browser something to talk to when it is offline, maybe we
can solve that problem too. Enter Google Gears.

Enabling Offl ine with Google Gears

Google Gears (http://code.google.com/apis/gears/) is an open-source browser extension that
provides developers with the ability to build Web applications using familiar technologies like
JavaScript that can run offline. Google Gears is composed of three components:


  • A local Web server Caches and serves the resource components of the Web
    application (XHTML, CSS, JavaScript, images, and so on) locally in absence of a
    connection to the Internet.

  • A database Stores data used by our offline applications with an instance of the
    open source SQLite database (www.sqlite.org), a fully capable relational database.

  • A worker pool extension Speeds up the processing model of JavaScript, allowing
    resource-intensive operations to happen asynchronously—in other words, to run in
    the background.


With these three components installed and enabled, you should be able to perform the
necessary functions to go offline.
Not everyone is going to have Gears installed, so after you include the Gears library in
your code, you will run a simple detection script and bounce them over to the Gears site for
installation.

<script type="text/javascript" src="gears_init.js"></script>
<script type="text/javascript">
/* global detect for gears */
if (!window.google || !google.gears)
{
location.href = "http://gears.google.com/?action=install&message=You need
Gears to run the Ajax: The Complete Reference Chapter 10 offline demos" +
"&return=http://ajaxref.com";
}
</script>
Free download pdf