Take your web applications offline with Google’s open-source browser add-in
Get started with Google Gears
The starting point for a Gears application is a Google-supplied Javascript file
called gears_init.js, which sets up a factory object from which you create other
Gears objects.
When your application starts, such as in the body.onload event handler, you can check whether Gears is installed and direct the user to the Gears install page if it is not.
I created a simple example, based on the code in Google’s tutorials. The startup function checks for Gears, and if present it creates a managed store and opens a SQLite database. Not much code is needed.
Here are the key lines:
//create a local server object
localServer = google.gears.factory.create(“beta.localserver”,”1.0”);
//create a managed store
store = localServer.createManagedStore(STORE_NAME);
//create a database
db = google.gears.factory.create(‘beta.database’, ‘1.0’);
//create or open a database table
if (db) {
db.open(‘pcw_gears’);
db.execute(‘create table if not exists pcw (somefield varchar(255))’);
}
A managed store is the starting point for caching content for offline use. You populate the managed store by creating a manifest identifying the files you need to cache. The manifest also has a version string. Then you can populate the store like this:
store.manifestUrl = pcw_manifest.json;
store.checkForUpdate();
At a later date the files on the server may change. In order to refresh the cached files, you update the version in the manifest. Next time the code calls checkForUpdate(), the new files will be downloaded.
Working with the database is equally straightforward. Here’s how to write a value to the database:
var inputEl = document.getElementById(‘dbInput’);
var inputval = inputEl.value;
db.execute(‘insert into pcw values (?)’, [inputval]);
and here is how you might get a value back:
var rs = db.execute(‘select somefield from pcw’);
var theValue;
if (rs.isValidRow())
{
theValue = rs.field(0);
textOut(“Database query successful”);
}
This example gets a single value, but the recordset object may contain many rows that you can step through. The advantage of SQLite is its support for standard SQL, along with fast performance.
These may be simple examples, but Gears is a revolution, giving web developers an infrastructure for offline web applications for the first time.
Related articles
Q.Why are some of the keys on my keyboard doing strange...
Q.Is my phone’s Bluetooth any use?
Q.Can I switch boot drives so that I can work on older...
Old Street roundabout is being touted by the Government as the UK's answer to Silicon Valley, but it seems our best innovations are coming from all over the UK
|
|
|
|
|
Computeractive Excel (2010) Online tutorialPrice: £19.99 |
Computeractive Word (2010) Online TutorialPrice: £19.99 |
Computeractive Powerpoint (2010) Online TutorialPrice: £19.99 |
Angry BirdsPrice: £9.99 |
Back Issue CD-Rom 14 (2011)Price: £15.99 |