AJAX - The Complete Reference

(avery) #1

PART III


Chapter 10: Web Services and Beyond 541


We provide a full example to test the storage mechanism at http://ajaxref.com/ch10/
gearsstorage.html, but you can inspect the full code here as well.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="gears_init.js"></script>
<title>Chapter 10 : Google Gears Offline Browsing</title>
</head>
<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/ch10/gearsstorage.html";
}
var localServer;
var store;
function initGears()
{
localServer = google.gears.factory.create("beta.localserver", "1.1");
store = localServer.createManagedStore("lockbox");
}
function createStore()
{
store.manifestUrl = "http://ajaxref.com/ch10/manifest.json";
store.checkForUpdate();
var timerId = window.setInterval(function() {
if (store.currentVersion)
{
window.clearInterval(timerId);
document.getElementById("responseOutput").innerHTML ="The documents
are now available offline.";
}
else if (store.updateStatus == 3)
document.getElementById("responseOutput").innerHTML =
"Error: " + store.lastErrorMessage;
}, 500);
}

function removeStore()
{
localServer.removeManagedStore("lockbox");
document.getElementById("responseOutput").innerHTML ="The local store has been
removed. You will no longer be able to browse offline.";
}
window.onload = function(){
initGears();
document.getElementById("captureBtn").onclick = function() {createStore();};
document.getElementById("eraseBtn").onclick = function() {removeStore();};
}
Free download pdf