收藏到: Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网 QQ书签 更多 Bookmark and Share
显示标签为“Gear”的博文。显示所有博文
显示标签为“Gear”的博文。显示所有博文

2009年4月18日星期六

Gear Factory

The Factory class is used to instantiate all other Gears objects. Using the create method, an application specifies the interface version it uses.

Code Example:
/ Check whether Gears is installed. if (window.google && google.gears) {   // Instantiate Gears objects   var db = google.gears.factory.create('beta.database');   db.open(); }

The supported class names are:

classNameGears class created
beta.databaseDatabase
beta.desktopDesktop
beta.geolocationGeolocation
beta.httprequestHttpRequest
beta.localserverLocalServer
beta.timerTimer
beta.workerpoolWorkerPool




Gear Database API 使用

Database API 提供铜鼓JavaScript使用的本地浏览器可以使用的数据。Gears使用SQLite数据库系统。

数据存储使用 same-origin security policy,意思是在这个domian之外的web应用无法访问到这个数据。Gears包括SQLite full-text-search extension fts2.

安全考虑:
  SQL语句通过execute()执行,并且应该用绑定参数的方式避免 SQL注入攻击(SQL injection attacks)。

许可:
  这些API要求用户许可。如果你要客户化缺省的dialog,你要显示调用google.gears.factory.getPermission()

Classes:
Database
ResultSet

修改本地SQLite库
SQLite通过Attache和Detach命令使用本地SQLite库。出于安全考虑,Gears禁止使用这些命令。将来在满足same-origin security policy情况下可以使用这些功能。

Pragma设置
  SQLite的Pragma命令允许在不同的平台下进行一些设置。出于安全考虑,目前禁止使用。
  Gears使用的Pragma设置:
  Pragma encoding=“UTF-8”
  Pragma auto_vacuum=1
  Pragma page_size=4096
  Pragma cache_size=2048
  Pragma synchronous=NORMAL
 
Full-Text Search
  Gears 包含 SQLite extension called fts2, for "Full-Text Search". fts2允许创建表,搜索Text数据。 fts2表创建:
 db.execute('CREATE VIRTUAL TABLE recipe USING fts2(dish, ingredients)');
 创建表recipe,字段dish、ingredients。所有fts2字段都是TEXT类型。表中的数据通过标准的SQL命令操作,如:INSERT/UPDATE/DELETE,和其他的表一样。fts2表有一个隐含的rowid字段,表现向唯一索引。

------------------------------------------------------------------
基本使用:

< type="text/javascript" src="gears_init.js">< /script>
< type="text/javascript">
var db = google.gears.factory.create('beta.database');
db.open('database-test');  // 创建数据库
db.execute('create table if not exists Test' +' (Phrase text, Timestamp int)');  // 创建表
db.execute('insert into Test values (?, ?)', ['Monkey!', new Date().getTime()]); // 插入数据
var rs = db.execute('select * from Test order by Timestamp desc');  // 查询

while (rs.isValidRow()) { // 是否到记录结束
  alert(rs.field(0) + '@' + rs.field(1));  // 获取字段值
  rs.next();  // 移动到下一条记录
}
rs.close();  // 关闭数据库
< /script>



Gear 体系结构

开发Gears程序,要按照离线程序体系结构开发做一些改变,

独立的数据层(Isloating the Data Layer)
普通的应用没有真正的数据层:



第一步,要独立数据层:

例如:如果Ajax也能够用通过JSON请求获取信息,要改成通过一个中间对象来获取数据。
这个对象决定是从服务器上、本地、或者同时从这2者中获取数据。

也可以考虑这个中间对象作为 data switch, 实现 Data Layer的接口
第一步通过这个data switch向Data Layer获取数据

第二步:如果增加了本地数据层,有Data switch进行切换:



Modality(特征?形式?)
最基本的离线使用的问题就是要确定Modality
  • Modal Application:相对于Online Modes,是很明确地离线使用,通过用户界面可以表现出来。用户很清楚地知道现在的使用模式,手工进行切换
  • Modeless Application:在online Mode和Offline Mode使用对用户是透明的,界面没有什么变化。用户不需要切换状态,是程序自动完成的。
Modal:
  当在线使用,和后台服务器进行通讯;当离线使用,和本地服务器进行通讯。数据必须在切换模式的时候进行同步。

Modeless:
 Modeless假设在离线状态下使用,或者随时都有可能离线。应用程序尽可能的使用本地数据(local store),并且不断地、小数据量通过后台服务和服务器进行数据同步。

数据同步(Data Synchronization)

有数种方式可以用在数据同步中,但是没有一种是适合任何情况的。最终选择的方案根据系统的不同而不同。

Manual Sync:
最简单的方式,有客户决定。可以通过将本地的所有数据进行上传覆盖服务器的数据,或者下载服务器上的数据覆盖本地的数据。

要求: 数据量不能大。

Background Sync:
   由应用程序在服务器和本地之间进行不断地同步。这个通过每隔一段和服务器进行通讯,让服务器push/stream数据到客户端(在Ajax术语中,叫做Comet)



Google Gear Manifest 文件

在Gear tutorial中的例子, Manifest包含以下内容:

{
  "betaManifestVersion": 1,
  "version": "my_version_string",
  "entries": [
      { "url": "go_offline.html"},
      { "url": "go_offline.js"},
      { "url": "../gears_init.js"}
    ]
}

其中:
version:定义一个版本的字符串,只要版本的内容不同,就会下载所有的enties中的内容 --- google文档上写这是一个已知的bug,目前无法做到增量更新

entries: 所有需要下载在本地的文件,可以进行离线浏览的文件,除了最后一个文件外,其他用comma进行分割
  文件的地址可以是完整的带域名的方式,也可以是相对Manifest文件的相对路径

要注意的:
1. 确信你的机器上已经安装了Gear (通过gear_init.js可以验证)
2.所有在Manifest中的文件必须URL正确,且存在可以获取到。Manifest文件和里面的URL在同一个Domain中
3.

2009年4月15日星期三

Google Gear 基本使用方法

Google Gear从例子上看,好像很容易使用(当然,仅仅是最基本的入门):

首先通过例子的gears_init.js生成一个Google Gears对象:google.gears
后面所有的操作通过这个对象进行。这个就直接抄gears_init.js实现,自己不用做任何处理了。

对google.gears对象的判断:
if (!window.google || !google.gears) {
    textOut("NOTE:  You must install Gears first."); --- 提示没有安装google gear,
  } else { ---- 安装了google gear,生成自己的localserver和store
    localServer = google.gears.factory.create("beta.localserver");
    store = localServer.createManagedStore(STORE_NAME);
    textOut("Yeay, Gears is already installed.");
  }

其中的:STORE_NAME就是一个自己随意取的名字;

MANIFEST的文件,在create store的时候使用到
  if (!window.google || !google.gears) { --- 判断是否安装了gear
    alert("You must install Gears first.");
    return;
  }

  store.manifestUrl = MANIFEST_FILENAME; --- 这个就是定义manifest文件名字的,而且mainfest的文件,要和当前的页面放在同一个目录中(如果没有设置目录的话)
  store.checkForUpdate();                                       --- 对store的创建更新

  var timerId = window.setInterval(function() {  --- 这是一个把更新的状态显示出来的脚本
    // When the currentVersion property has a value, all of the resources
    // listed in the manifest file for that version are captured. There is
    // an open bug to surface this state change as an event.
    if (store.currentVersion) {
      window.clearInterval(timerId);
      textOut("The documents are now available offline.\n" + 
              "With your browser offline, load the document at " +
              "its normal online URL to see the locally stored " +
       "version. The version stored is: " + 
              store.currentVersion);
    } else if (store.updateStatus == 3) {
      textOut("Error: " + store.lastErrorMessage);
    }
  }, 500);  


删除一个store也是相当容易的:
  if (!window.google || !google.gears) {
    alert("You must install Gears first.");
    return;
  }

  localServer.removeManagedStore(STORE_NAME); --- 删除,这个STORE_NAME就是上面创建时候的STORE NAME
  textOut("Done. The local store has been removed." +
          "You will now see online versions of the documents.");