<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dave Kelly :: Blog &#187; Code</title>
	<atom:link href="http://www.davidkelly.ie/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.davidkelly.ie</link>
	<description>www.davidkelly.ie</description>
	<lastBuildDate>Tue, 19 Apr 2011 22:13:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Getting into Gears: Offline Web App Tutorial &#8211; Part 1</title>
		<link>http://www.davidkelly.ie/2008/07/19/getting-into-gears-offline-web-app-tutorial-part-1/</link>
		<comments>http://www.davidkelly.ie/2008/07/19/getting-into-gears-offline-web-app-tutorial-part-1/#comments</comments>
		<pubDate>Sat, 19 Jul 2008 12:39:55 +0000</pubDate>
		<dc:creator>Dave</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Web Applications]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://www.davidkelly.ie/blog/?p=80</guid>
		<description><![CDATA[Growing numbers of web applications have been dipping their toes into providing offline access using Gears. Recent examples include Google Reader (Google originally developed Gears before releasing it as an open source project), Remember the Milk, WordPress (in Version 2.6), with planned integrations from MySpace and Gmail. Offline access provides a way for a user [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-full wp-image-102" style="width: 200px; height: 174px; float: right; margin-left: 10px; margin-bottom: 10px;" title="Gears" src="http://www.davidkelly.ie/wp-content/uploads/2008/07/gears.jpg" alt="Image of Gears" />Growing numbers of web applications have been dipping their toes into providing offline access using <a title="Gears Homepage" href="http://gears.google.com/">Gears</a>. Recent examples include <a href="http://www.google.com/reader/">Google Reader</a> (Google originally developed Gears before releasing it as an open source project), <a title="To-do Lists" href="http://www.rememberthemilk.com">Remember the Milk</a>, <a href="http://www.wordpress.org">WordPress</a> (in Version 2.6), with <a title="ReadWriteWeb article" href="http://www.readwriteweb.com/archives/google_gears_coming_to_gmail.php">planned integrations</a> from <a href="http://www.myspace.com">MySpace</a> and <a href="http://gmail.google.com">Gmail</a>.</p>
<p>Offline access provides a way for a user to interact with the application when an Internet connection is unavailable, something &#8220;traditional&#8221; web applications cannot do. Gears also provides a means to improve the performance of apps by storing all the files necessary for them to operate on the user&#8217;s computer (meaning no need for downloads of images, script files, css, etc). An example of this is shown below.<br />
<span id="more-80"></span><br />
Gears achieves this by providing a number of features through <a title="Gears Developer Site" href="http://code.google.com/apis/gears/">its API</a>:</p>
<ul>
<li>A &#8220;Localserver&#8221; capable of serving files stored on the user&#8217;s machine, rather than relying on a remote web server;</li>
<li>A local database (SqlLite) for storing information that can be queried &amp; manipulated locally;</li>
<li>Access to a &#8220;Workerpool&#8221; which allows multiple JavaScript processes to run simultaneously &#8211; something which allows scripts to run in the background without causing the user interface to slow down;</li>
<li>Other APIs available include access to HttpRequest (for use in Workerpools using AJAX), the ability to create desktop shortcuts, and a Timer.</li>
</ul>
<p>For Gears to be used, it must firstly be installed on the user&#8217;s machine. To protect users, they must opt-in to allow certain operations to be carried out (for example, installing a desktop shortcut, or creating a local database).</p>
<h3>An Example</h3>
<p>In an application under development, the user needs access to a set of data that is unique to them. The data changes infrequently, but is needed regularly as either a full set, or is queried based on different conditions. In a &#8220;traditional&#8221; web application, this means frequent calls to the server which takes time, bandwidth and server processing power.  However, Gears offers an alternative. By maintaining the data on the client side, and updating it as required, the burden is removed from the server. While using the Gears approach in this situation does allow for offline access, one of the main motivations was to increase the responsiveness of the application. So, how does it all work?</p>
<p>First off, you need to check Gears is installed and available:</p>
<p><code><br />
&lt;script src="gears_init.js"&gt;&lt;/script&gt; &lt;!-- This need to be included every time --&gt;<br />
&lt;script type="text/javascript"&gt;<br />
if (!window.google || !google.gears) {<br />
location.href = "http://gears.google.com/?action=install&amp;message=Please Install Gears ..." +"&amp;return=http://www.example.com/setup/gears/installed/";<br />
}<br />
else{<br />
var db;                // local database<br />
var localServer;    // localServer<br />
}// else<br />
</code><br />
You can see that all the code is in JavaScript &#8211; this makes sense because it is all being run by the user&#8217;s browser, not a web server. The &#8220;gears_init.js&#8221; script makes sure Gears is installed (available from <a href="http://code.google.com/apis/gears/design.html#detecting">Gears documentation site</a>).  If it&#8217;s not available, the user will be redirected to an install site, and then sent back to an address you specify (http://www.example.com/setup/gears/installed/). This script also creates a &#8220;Factory&#8221; object, which is used to create all other objects.</p>
<p>The next step is to create a database to use and add a table to it:<br />
<code>// Datbase Setup<br />
db = google.gears.factory.create('beta.database');<br />
db.open('treatmentDB');<br />
db.execute('create table if not exists treatment ' + '(treatment_id int primary key, treatment_name text, treatment_price decimal, category_id int, timestamp int, toUpdate text)');<br />
db.close();</code></p>
<p>This creates a database object, and opens the database &#8220;treatmentDB&#8221; (or creates it if it doesn&#8217;t exist). The &#8220;execute&#8221; command is used to run SQL commands (SqlLite version) against the database &#8211; in this case, if there is no table called treatment, create one. I also included two fields to ensure the database is up-to-date &#8211; a timestamp, and a toUpdate field (which holds true / false flag depending on whether a record has been added to the local database, but not yet updated in the remote database).</p>
<h3>What&#8217;s Next?</h3>
<p>In the second part of the tutorial, the database will be populated using data on the remote server. I&#8217;ll show an example of using a Workerpool to do this, so that the operation doesn&#8217;t affect the user interface. The data will then be queried and output.</p>
<p class="block-highlght">Update: <a title="Gears Tutorial Part 2" href="http://www.davidkelly.ie/blog/2008/08/20/getting-into-gears-offline-web-app-tutorial-part-2/">Part 2 of the Gears tutorial</a> is now available</p>
]]></content:encoded>
			<wfw:commentRss>http://www.davidkelly.ie/2008/07/19/getting-into-gears-offline-web-app-tutorial-part-1/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

