显示同步的优点是:
- 实现起来比较简单。
- 比较容易测试。
缺点是:
- 如果同步的数据块比较大,将会一次性消耗很长的时间,而 Web 应用程序在数据同步期间是不可用的,这将让用户等待很长时间。
- 在网络连接时断时续的情况下,用户将会疲于点击同步按钮来同步数据,用户还不得不对网络状况保持敏感以在适当的时候手工同步数据。
后台同步
在后台同步中,Web 应用程序持续地将本地数据与服务器端数据进行同步,同步操作是在后台进行的,不需要用户显式地触发,同步过程中用户依然可以使用 Web 应用程序,实现上可以用 WorkerPool 在后台设定每隔一段时间跟服务器同步一次。
后台同步的优点是:
- 同步操作对用户是透明的,用户不需要做任何操作,也不需要对网络状况保持敏感。
- 同步操作一直在后台进行,即时当网络意外中断的情况下,本地数据和服务器数据都能保持较高的一致性。
缺点是:
- 实现起来比较复杂。
- 同步是在后台进行的,不太容易进行测试。
- 同步的间隔不能太频繁,否则会产生太多的 HTTP 连接,从而降低服务器的响应能力。
![]() ![]() |
使用 Desktop API
Desktop API 能帮你在用户桌面上创建你的 Web 应用程序的快捷方式,调用代码如清单 11 所示。
清单 11. 创建桌面快捷方式
// 创建 Desktop 对象 var desktop = google.gears.factory.create('beta.desktop'); // 创建桌面快捷方式 desktop.createShortcut('Google Gears Desktop API Example', 'http://www.testapp.net/gears/DesktopAPI.html', { '128x128':'http://www.testapp.net/gears/test128.png', '48x48': 'http://www.testapp.net/gears/test48.png', '32x32': 'http://www.testapp.net/gears/test32.png', '16x16': 'http://www.testapp.net/gears/test16.png'}, 'Google Gears Desktop API @www.testapp.net'); |
createShortcut 方法有以下几个参数 :
- name, 这是用户看到的快捷方式的名字,在清单 11 中设为 "Google Gears Desktop API Example"
- url, 当用户启动快捷方式时要访问的 Web 应用程序的 url, 在清单 11 中设为" http://www.testapp.net/gears/DesktopAPI.html "
- icons, 这是一个 JSON 对象,可以设定不同尺寸的图标,在清单 11 中设定了 4 中不同尺寸的图标
- description, 这是一个可选参数,用于描述将要创建的这个快捷方式的更详细信息,当清单 11 的代码在用户浏览器里执行时,会弹出一个确认对话框,description 参数确定的文本将会显示在该确认对话框里面,如图 1 所示。
当清单 11 所示代码在 Window 上的浏览器执行的时候,不管是 Internet Explorer 还是 Firefox, 都会出现如图 1 所示的确认对话框,让用户选择是否需要在桌面,开始菜单和快速启动栏里面创建 Web 应用程序的快捷方式。
图 1. 创建桌面快捷方式

![]() ![]() |
使用 Geolocation API
Geolocation API 使你的 Web 应用程序能获取用户的当前位置,结合 Google Maps API 可以立刻在 Google 地图上显示用户当前的位置,代码如清单 12 所示。
清单 12. 结合使用 Geolocation API 和 Google Maps API
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"/> <title>Google Maps API and Google Gear Geolocation API Example</title> <script src="http://ditu.google.cn/maps?file=api&v=2&key=<your_map_api_key>" type="text/javascript"></script> <script type="text/javascript" src="gears_init.js"></script> <script type="text/javascript"> // 创建 Geolocation 对象 var geo = google.gears.factory.create('beta.geolocation'); var map; function updatePosition(p) { alert('Current lat/lon is: ' + p.latitude + ',' + p.longitude); // 在地图中显示用户当前位置 map.setCenter(new GLatLng(p.latitude,p.longitude),13); } function handleError(positionError) { alert('Attempt to get location failed: ' + positionError.message); } function load() {// 初始化地图 if (GBrowserIsCompatible()) { map = new GMap2(document.getElementById("map")); map.addControl(new GLargeMapControl()); map.addControl(new GSmallZoomControl()); map.addControl(new GScaleControl()); map.addControl(new GMapTypeControl()); map.addControl(new GOverviewMapControl()); map.setCenter(new GLatLng(39.617, 116.197), 13); } } function gotoMyposition(){ // 获取用户当前位置 geo.getCurrentPosition(updatePosition, handleError); } </script> </head> <body onload="load()" onunload="GUnload()"> <h1>Hello,Google Maps API and Google Gears Geolocation API</h1> <div id="map" style="width: 800px; height: 600px"></div> <br/> <input type="button" value="Go to my position" onClick="gotoMyposition()"/> </body> </html> |
上面代码中的关键点是用 geo.getCurrentPosition(updatePosition, handleError) 来获取用户的当前位置,当 Google Gears 拿到用户的当前位置信息后,将其值传给 updatePosition 函数来处理,该函数调用 Google Maps API 将地图的中心位置设为用户的当前位置。如果 Google Gears 拿不到用户的当前位置,将会调用 handleError 函数来处理错误信息。
![]() ![]() |
使用 HttpRequest API
Google Gears 提供的 HttpRequest API 实现了 W3C XmlHttpRequest specification 的一个子集 , 你可以用 HttpRequest API 发起一个 Ajax 请求。你肯定会问在浏览器中我们已经可以用 XmlHttpRequest 对象来创建 Ajax 请求,为什么 Google Gears 还要提供另外一种方式呢?这是因为在前面提到的子 worker 里面是不能访问浏览器的 XmlHttpRequest 对象,所以 Google Gears 提供了 HttpRequest API 使得子 worker 也能创建 Ajax 请求,当然 , 你依然可以在普通网页里面使用 HttpRequest API,而不是非得到子 worker 里面才能用。为了简单起见,我们只介绍如何在普通网页里面使用 HttpRequest API,如清单 13 所示
清单 13. 使用 HttpRequest API
<html> <script type="text/javascript" src="gears_init.js"></script> <script type="text/javascript"> // 创建 HttpRequest 对象 var request = google.gears.factory.create('beta.httprequest'); function handleResult(){ //readyState 为 4 表示 HttpRequest 请求的状态是已被服务器响应 if (request.readyState == 4) { var con = document.getElementById("content"); // 从 HttpRequest 请求中读取响应信息并显示在网页上 con.innerHTML = request.responseText; } } function get(){ // 确定 HttpRequest 对象要访问的 Web 资源和访问方式 , 要访问的资源必须在同一个域下面 request.open('GET', 'http://www.testapp.net/gears/test.txt'); // 设置请求状态改变时的事件处理函数 request.onreadystatechange = handleResult; var con = document.getElementById("content"); con.innerHTML = "Waiting..."; // 发送 HttpRequest 请求 request.send(); } </script> <body> <h1>Hello, Google Gears Request API</h1> <div id="content"></div> <br/> <input type="button" value="Fetch content of test.txt" onClick="get()"/> <input type="button" value="Clear" onClick="document.getElementById('content').innerHTML = ''"/> </body> </html> |
需要注意的是当用 HttpRequest 的 open 方法确定要访问的 Web 资源时必须遵守同源策略,否则浏览器将报错。
![]() ![]() |