#眉標=AJAX #副標=AJAX開發工具(11) #大標=GWT結合Gears開發離線網頁應用程式 #作者=文圖/沈炳宏 ============= 程式1 "entries": [ { "url": "index.html" }, { "url": "style.css" }, { "url": "resources/logo.gif" }, { "url": "gears_init.js"}, { "url": "go_offline.html"}, { "url": "go_offline.js"} ] ================ ============= 程式2 Enable Offline Usage ================ ============= 程式3 var STORE_NAME = "my_offline_docset"; var MANIFEST_FILENAME = "tutorial_manifest.json"; var localServer; var store; function init() { if (!window.google || !google.gears) { textOut("NOTE: You must install Google Gears first."); } else { localServer = google.gears.factory.create( "beta.localserver","1.0"); store = localServer.createManagedStore( STORE_NAME); textOut( "Yeay, Google Gears is already installed."); } } function createStore() { if (!window.google || !google.gears) { alert("You must install Google Gears first."); return; } store.manifestUrl = MANIFEST_FILENAME; store.checkForUpdate(); var timerId = window.setInterval(function() { 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); } ================ ============= 程式4 ================ ============= 程式5 public class DatabaseDemo implements EntryPoint { private static native boolean isGearsInstalled() /*-{ try { return $wnd.google.gears.factory != null; } catch (e) { return false; } }-*/; private final Button button = new Button("Add"); private Database db; private final TextBox input = new TextBox(); private final Label[] labels = new Label[] { new Label(), new Label(), new Label()}; public void onModuleLoad() { assert (isGearsInstalled()); RootPanel rootPanel = RootPanel.get(); rootPanel.add(input); rootPanel.add(button); for (int i = 0; i < labels.length; ++i) { rootPanel.add(labels[i]); } try { db = new Database("database-demo"); db.execute( "create table if not exists Demo ( Phrase varchar(255), Timestamp int)"); } catch (GearsException e) { Window.alert(e.toString()); } button.addClickListener(new ClickListener() { displayRecentPhrases(); } void displayRecentPhrases() { try { ResultSet rs = db.execute( "select * from Demo order by Timestamp desc"); for (int i = 0; rs.isValidRow(); ++i, rs.next()) { if (i < labels.length) { labels[i].setText(rs.getFieldAsString(0)); } else { db.execute( "delete from Demo where Timestamp=?", new String[] {rs.getFieldAsString(1)}); } } rs.close(); } catch (DatabaseException e) { Window.alert(e.toString()); } } } ================