122
Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 Stephan Hochdörfer, bitExpert AG

Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Offline. Na und? Strategien für offlinefähige

Applikationen in HTML5Stephan Hochdörfer, bitExpert AG

Page 2: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Offline. Na und? - Strategien für offlinefähige Applikationen

Über mich

Stephan Hochdörfer

Head of IT der bitExpert AG, Mannheim

[email protected]

@shochdoerfer

Page 3: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Offline. Na und? - Strategien für offlinefähige Applikationen

Daten auf dem Client speichern?

Page 4: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Offline. Na und? - Strategien für offlinefähige Applikationen

Wie wurde dies in der Vergangenheit gelöst?

Page 5: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Offline. Na und? - Strategien für offlinefähige Applikationen

Cookies. Lecker aber mehr auch nicht.

Page 6: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Offline. Na und? - Strategien für offlinefähige Applikationen

IE DHTML Behaviours

Page 7: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Offline. Na und? - Strategien für offlinefähige Applikationen

Flash Cookies schmecken auch lecker.

Page 8: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Offline. Na und? - Strategien für offlinefähige Applikationen

Google Gears mit neuem Ansatz.

Page 9: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Gibt es nichts wirklich sinnvolles?

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 10: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Offline. Na und? - Strategien für offlinefähige Applikationen

als Lösung...

Page 11: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Offline. Na und? - Strategien für offlinefähige Applikationen

[...] we take the next step, announcing 2014 as the target for

Recommendation.Jeff Jaffe, Chief Executive Officer, World Wide Web Consortium

Page 12: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 13: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 14: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Was bedeutet „offline“?

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 15: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Was bedeutet „offline“?

Offline. Na und? - Strategien für offlinefähige Applikationen

Applikation vs. Content

Page 16: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Was bedeutet „offline“?

Offline. Na und? - Strategien für offlinefähige Applikationen

Application Cache vs. Offline Storage

Page 17: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache für statische Ressourcen

Offline. Na und? - Strategien für offlinefähige Applikationen

<!DOCTYPE html><html lang="en">

HTML Page:

Page 18: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

<!DOCTYPE html><html lang="en" manifest="cache.manifest">

App Cache für statische Ressourcen

Offline. Na und? - Strategien für offlinefähige Applikationen

HTML Page:

cache.manifest (Content-Type: text/cache-manifest):

CACHE MANIFEST

js/app.jscss/app.cssfavicon.icohttp://someotherdomain.com/image.png

Page 19: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache für statische Ressourcen

Offline. Na und? - Strategien für offlinefähige Applikationen

CACHE MANIFEST# 2012-09-16

NETWORK:data.php

CACHE:/main/home/main/app.js/settings/home/settings/app.jshttp://myhost/logo.pnghttp://myhost/check.pnghttp://myhost/cross.png

Page 20: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache für statische Ressourcen

Offline. Na und? - Strategien für offlinefähige Applikationen

CACHE MANIFEST# 2012-09-16

FALLBACK:/ /offline.html

NETWORK:*

Page 21: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache Scripting

Offline. Na und? - Strategien für offlinefähige Applikationen

// events fired by window.applicationCachewindow.applicationCache.onchecking = function(e) {log("Checking for updates");}window.applicationCache.onnoupdate = function(e) {log("No updates");}window.applicationCache.onupdateready = function(e) {log("Update ready");}window.applicationCache.onobsolete = function(e) {log("Obsolete");}window.applicationCache.ondownloading = function(e) {log("Downloading");}window.applicationCache.oncached = function(e) {log("Cached");}window.applicationCache.onerror = function(e) {log("Error");}

// Log each filewindow.applicationCache.onprogress = function(e) { log("Progress: downloaded file " + counter); counter++;};

Page 22: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache Scripting

Offline. Na und? - Strategien für offlinefähige Applikationen

// Check if a new cache is available on page load.window.addEventListener('load', function(e) { window.applicationCache.addEventListener('updateready', function(e) {

if(window.applicationCache.status == window.applicationCache.UPDATEREADY) { // Browser downloaded a new app cache. // Swap it in and reload the page window.applicationCache.swapCache(); if (confirm('New version is available. Load it?)) { window.location.reload(); } } else { // Manifest didn't change... } }, false);

}, false);

Page 23: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache – Einige Fallstricke!

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 24: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache – Einige Fallstricke!

Offline. Na und? - Strategien für offlinefähige Applikationen

1. Dateien werden immer(!) vom lokalen Cache ausgeliefert.

Page 25: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache – Einige Fallstricke!

Offline. Na und? - Strategien für offlinefähige Applikationen

2. Der lokale Cache wird nur dann aktualisiert wenn sich die manifest

Datei geändert hat.

Page 26: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache – Einige Fallstricke!

Offline. Na und? - Strategien für offlinefähige Applikationen

3. Nicht ladbare Dateien aus der CACHE Sektion führen dazu dass der

Cache invalide ist.

Page 27: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache – Einige Fallstricke!

Offline. Na und? - Strategien für offlinefähige Applikationen

4. Kann die manifest Datei nicht geladen werden, erfolgt kein Caching!

Page 28: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache – Einige Fallstricke!

Offline. Na und? - Strategien für offlinefähige Applikationen

5. Nicht gecachte Ressourcen werden auf einer gecachten Seite nicht

angezeigt.

Page 29: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache – Einige Fallstricke!

Offline. Na und? - Strategien für offlinefähige Applikationen

6. Nach Aktualisierung des Caches muss die Seite neu geladen werden!

Page 30: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache – Einige Fallstricke!

Offline. Na und? - Strategien für offlinefähige Applikationen

7. Mit expires Header arbeiten um das Cachen des manifests zu verhinden!

Page 31: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache – Cache Manifest Validator

Offline. Na und? - Strategien für offlinefähige Applikationen

http://manifest-validator.com

Page 32: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache – Was darf gecacht werden?

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 33: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache – Was darf gecacht werden?

Offline. Na und? - Strategien für offlinefähige Applikationen

Ja: Schriften Startbild Applikationsicon Einstiegsseite Fallbackseite

Nein: CSS HTML Javascript

Page 34: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

App Cache – Was darf gecacht werden?

Offline. Na und? - Strategien für offlinefähige Applikationen

Den App Cache nur für „statischen Content“ verwenden!

Page 35: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Ausflug: Data URI Schema

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 36: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

<!DOCTYPE HTML><html> <head>  <title>The Data URI scheme</title>  <style type="text/css">  ul.checklist li {    margin­left: 20px;    background: white url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==') no­repeat scroll left top;}  </style> </head> <body>  <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot"> </body></html>

Ausflug: Data URI Schema

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 37: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Dynamische Daten lokal speichern...

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 38: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Offline. Na und? - Strategien für offlinefähige Applikationen

Beispiel: Todoliste

Page 39: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Dynamische Daten lokal speichern...

Offline. Na und? - Strategien für offlinefähige Applikationen

Sourcen:github.com/bitExpert/html5-offline

Page 40: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Dynamische Daten lokal speichern...

Offline. Na und? - Strategien für offlinefähige Applikationen

Web Storage, Web SQL Database, IndexedDB, File API

Page 41: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 42: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage

Offline. Na und? - Strategien für offlinefähige Applikationen

Komfortable Art Daten offline zu speichern: Key/Value Speicher

Page 43: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: 2 Arten

Offline. Na und? - Strategien für offlinefähige Applikationen

localStorage vs. sessionStorage

Page 44: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Datensatz hinzufügen

Offline. Na und? - Strategien für offlinefähige Applikationen

function add(item) {try {

// for a new item set idif((typeof item.id === "undefined")

|| (null == item.id) || ("" == item.id)) {item.id = get_lastIndex() + 1;

}

// store object as stringlocalStorage.setItem(item.id,

JSON.stringify(item) );

// update the indexset_lastIndex(item.id);

}catch(ex) {

console.log(ex);}

}

Page 45: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Datensatz ändern

Offline. Na und? - Strategien für offlinefähige Applikationen

function modify(item) {try {

// store object as stringlocalStorage.setItem(item.id,

JSON.stringify(item) );

}catch(ex) {

console.log(ex);}

}

Page 46: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Datensatz löschen

Offline. Na und? - Strategien für offlinefähige Applikationen

function remove (id) {try {

localStorage.removeItem(id);}catch(ex) {

console.log(ex);}

}

Page 47: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Datensätze auslesen

Offline. Na und? - Strategien für offlinefähige Applikationen

function read() { try {

var lastIdx = get_lastIndex(); for(var i = 1; i <= lastIdx; i++) {

if(null !== localStorage.getItem(i)) { // parse and render item var item = JSON.parse(

localStorage.getItem(i) );

} }

} catch(ex) {

console.log(ex); }}

Page 48: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Wie sessionStorage nutzen?

Offline. Na und? - Strategien für offlinefähige ApplikationenOffline. Na und? - Strategien für offlinefähige Applikationen

Page 49: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Wie sessionStorage nutzen?

Offline. Na und? - Strategien für offlinefähige Applikationen

Einfach: Ersetze „localStorage“durch „sessionStorage“

Page 50: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Datensatz hinzufügen

Offline. Na und? - Strategien für offlinefähige Applikationen

function add(item) {try {

// for a new item set idif((typeof item.id === "undefined")

|| (null == item.id) || ("" == item.id)) {item.id = get_lastIndex() + 1;

}

// store object as stringsessionStorage.setItem(item.id,

JSON.stringify(item) );

// update the indexset_lastIndex(item.id);

}catch(ex) {

console.log(ex);}

}

Page 51: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: sessionStorage Fallstrick

Offline. Na und? - Strategien für offlinefähige Applikationen

Jedes Tab / Fenster bringt eigenen sessionStorage mit sich!

Page 52: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Alternative Zugriffsmöglichkeiten

Offline. Na und? - Strategien für offlinefähige Applikationen

var value = "my value";

// method calllocalStorage.setItem("key", value);

// Array accessorlocalStorage[key] = value;

// Property accessorlocalStorage.key = value;

Page 53: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Offline. Na und? - Strategien für offlinefähige Applikationen

Welche Daten sind gespeichert?

Page 54: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Vorteile

Offline. Na und? - Strategien für offlinefähige Applikationen

Die meisten der aktuellen Browser „können“ Web Storage.

Page 55: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Vorteile

Offline. Na und? - Strategien für offlinefähige Applikationen

Funktioniert sogar im IE8 :)

Page 56: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Vorteile

Offline. Na und? - Strategien für offlinefähige Applikationen

Kein HTTP Overhead wie bsp. bei Cookies.

Page 57: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Nachteile

Offline. Na und? - Strategien für offlinefähige Applikationen

Daten werden unstrukturiert gespeichert.

Page 58: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Nachteile

Offline. Na und? - Strategien für offlinefähige Applikationen

Nicht transaktionsfähig!

Page 59: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Nachteile

Offline. Na und? - Strategien für offlinefähige Applikationen

Nicht asynchron!

Page 60: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Nachteile

Offline. Na und? - Strategien für offlinefähige Applikationen

Daten können nicht automatisch verfallen. Manueller Aufwand nötig.

Page 61: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Nachteile

Offline. Na und? - Strategien für offlinefähige Applikationen

Unzureichende Informationen wie voll der lokale Cache wirklich ist.

Page 62: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web Storage: Nachteile

Offline. Na und? - Strategien für offlinefähige Applikationen

http:// und https:// definierenunterschiedliche Storages.

Page 63: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web SQL Database

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 64: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web SQL Database

Offline. Na und? - Strategien für offlinefähige Applikationen

Eine lokale SQL Datenbank auf SQLite Basis.

Page 65: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web SQL Database: Callbacks

Offline. Na und? - Strategien für offlinefähige Applikationen

var onError = function(tx, ex) {alert("Error: " + ex.message);

};

var onSuccess = function(tx, results) {var len = results.rows.length;

for(var i = 0; i < len; i++) {// render found todo itemrender(results.rows.item(i));

}};

Page 66: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web SQL Database: Datenbank erzeugen

Offline. Na und? - Strategien für offlinefähige Applikationen

// initalize the database connectionvar db = openDatabase('todo', '1.0', 'Todo Database', 5 * 1024 * 1024 );

db.transaction(function (tx) {tx.executeSql(

'CREATE TABLE IF NOT EXISTS todo '+ '(id INTEGER PRIMARY KEY ASC, todo TEXT)',

[], onSuccess, onError

);});

Page 67: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web SQL Database: Datenbankmigration

Offline. Na und? - Strategien für offlinefähige Applikationen

function m1(t){ t.executeSql("create table tbl1...") }function m2(t){ t.executeSql("alter table tbl1...") }function m3(t){ t.executeSql("alter table tbl1...") } if(db.version == "") { db.changeVersion("", "1", m1, null, function() { db.changeVersion("1", "2", m2, null, function() { db.changeVersion("2", "3", m3); }); });} if(db.version == "1") { db.changeVersion("1", "2", m2, null, function() { db.changeVersion("2", "3", m3); });} if(db.version == "2") { db.changeVersion("2", "3", m3);}

Page 68: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web SQL Database: Datensatz hinzufügen

Offline. Na und? - Strategien für offlinefähige Applikationen

function add(item) {db.transaction(function(tx) {

tx.executeSql('INSERT INTO todo (todo) VALUES (?)',[

item.todo],onSuccess,onError

);});

}

Page 69: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web SQL Database: Datensatz verändern

Offline. Na und? - Strategien für offlinefähige Applikationen

function modify(item) {db.transaction(function(tx) {

tx.executeSql('UPDATE todo SET todo = ? WHERE id = ?',[

item.todoitem.id

],onSuccess,onError

);});

}

Page 70: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web SQL Database: Datensatz löschen

Offline. Na und? - Strategien für offlinefähige Applikationen

function remove(id) {db.transaction(function (tx) {

tx.executeSql('DELETE FROM todo WHERE id = ?',[

id],onSuccess,onError

);});

}

Page 71: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web SQL Database: Datensätze auslesen

Offline. Na und? - Strategien für offlinefähige Applikationen

function read() {db.transaction(function (tx) {

tx.executeSql('SELECT * FROM todo',[],onSuccess,onError

);});

}

Page 72: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web SQL Database: Vorteile

Offline. Na und? - Strategien für offlinefähige Applikationen

Eine SQL Datenbank im Browser!

Page 73: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web SQL Database: Nachteile

Offline. Na und? - Strategien für offlinefähige Applikationen

Eine SQL Datenbank im Browser!

Page 74: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web SQL Database: Nachteile

Offline. Na und? - Strategien für offlinefähige Applikationen

SQLite kann seeeehr langsam sein!

Page 75: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web SQL Database: Nachteile

Offline. Na und? - Strategien für offlinefähige Applikationen

Nicht länger Teil der HTML5 Spezifikation!

Page 76: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 77: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB

Offline. Na und? - Strategien für offlinefähige Applikationen

Kompromiss aus Web Storage und Web SQL Database.

Page 78: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB

Offline. Na und? - Strategien für offlinefähige Applikationen

Die NoSQL Datenbank fürden Browser.

Page 79: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Web SQL Database vs. IndexedDB

Offline. Na und? - Strategien für offlinefähige Applikationen

Kategorie Web SQL IndexedDB

Speicherart Tabellen mit Spalten und Zeilen

Objectstore mit Javascript Objekten und Keys

Abfragemechanismus

SQL Cursor APIs, Key Range APIs und Applicationslogik

Transaktionalität

Lock für Databanken, Tabellen oder Zeilen bei READ_WRITE Transaktionen

Locks für Datenbanken (VERSION_CHANGE Transaktion) und Objectstores (READ_ONLY, READ_WRITE Transaktion).

Transaktions- Commits

Transaktionen werden explizt erzeugt. Standard: Rollback, außer es wird explizit ein commit ausgeführt.

Transaktionen werden explizt erzeugt. Standard: Committen sofern kein Fehler aufgetreten ist.

Page 80: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB: Vorarbeiten

Offline. Na und? - Strategien für offlinefähige Applikationen

// different browsers, different naming conventionsvar indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;

var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;

var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;

Page 81: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB: Objektspeicher erzeugen

Offline. Na und? - Strategien für offlinefähige Applikationen

var db = null;var request = indexedDB.open("todo");request.onfailure = onError;request.onsuccess = function(e) {

db = request.result;var v = "1.0";if(v != db.version) {

var verRequest = db.setVersion(v);verRequest.onfailure = onError;verRequest.onsuccess = function(e) {

var store = db.createObjectStore("todo",{

keyPath: "id",autoIncrement: true

});e.target.transaction.oncomplete =

function() {};};

}};

Page 82: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB: Datensatz hinzufügen

Offline. Na und? - Strategien für offlinefähige Applikationen

function add(item) {try {

var trans = db.transaction(["todo"], IDBTransaction.READ_WRITE);

var store = trans.objectStore("todo");var request = store.put({

"todo": item.todo,});

}catch(ex) {

onError(ex);}

}

Page 83: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB: Datensatz verändern

Offline. Na und? - Strategien für offlinefähige Applikationen

function modify(item) {try {

var trans = db.transaction(["todo"], IDBTransaction.READ_WRITE);

var store = trans.objectStore("todo");var request = store.put(item);

}catch(ex) {

onError(ex);}

}

Page 84: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB: Datensatz löschen

Offline. Na und? - Strategien für offlinefähige Applikationen

function remove(id) {try {

var trans = db.transaction(["todo"], IDBTransaction.READ_WRITE);

var store = trans.objectStore("todo");var request = store.delete(id);

}catch(ex) {

onError(ex);}

}

Page 85: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB: Datensätze auslesen

Offline. Na und? - Strategien für offlinefähige Applikationen

function read () {try {

var trans = db.transaction(["todo"], IDBTransaction.READ);

var store = trans.objectStore("todo");var keyRange = IDBKeyRange.lowerBound(0);var cursorRequest = store.openCursor(keyRange);

cursorRequest.onsuccess = function(e) {var result = e.target.result;if(!!result == false) {

return;}// @TODO: render result.valueresult.continue();

};}catch(ex) {

onError(ex);}

}

Page 86: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB: Datensatz mittels Index auslesen

Offline. Na und? - Strategien für offlinefähige Applikationen

try {var index = db.openIndex('todo');var item = index.get(1337);

}catch(ex) {

onError(ex);}

Page 87: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB: Index erzeugen

Offline. Na und? - Strategien für offlinefähige Applikationen

try {// create non-unique index db.createIndex("Created", "createdDate",

{"unique": false});

// create unique index db.createIndex("OtherKey", "otherField",

{"unique": true});

// create multi-column index (not working in IE10!) db.createIndex("MultiIndex", ["field1", "field2"]);

}catch(ex) {

onError(ex);}

Page 88: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB: Zu kompliziert?

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 89: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB: Zu kompliziert?

Offline. Na und? - Strategien für offlinefähige Applikationen

db.js als Wrapper nutzen

Page 90: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB: db.js als Wrapper

Offline. Na und? - Strategien für offlinefähige Applikationen

var server;db.open( { server: 'todo', version: 1, schema: {

todo: { key: { keyPath: 'id' , autoIncrement: true}, indexes: {

Created: { } } }

}}).done(function(s) { server = s;});

Page 91: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB: db.js als Wrapper

Offline. Na und? - Strategien für offlinefähige Applikationen

server.todo.add( { todo: 'This is a sample todo item'}).done(function(item) { // item stored});

Page 92: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB: db.js als Wrapper

Offline. Na und? - Strategien für offlinefähige Applikationen

server.todo.query() .execute() .done(function(results) { // do something with the results });

Page 93: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB: Vorteile

Offline. Na und? - Strategien für offlinefähige Applikationen

Neuer Standard der künftig durch viele Browser unterstützt wird.

Page 94: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB: Nachteile

Offline. Na und? - Strategien für offlinefähige Applikationen

Nicht alle mobilen Browseruntersützen IndexedDB.

Page 95: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

IndexedDB Polyfill

Offline. Na und? - Strategien für offlinefähige Applikationen

IndexedDB Emulation via WebSQL:http://nparashuram.com/IndexedDBShim

Page 96: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

File API

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 97: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

File API

Offline. Na und? - Strategien für offlinefähige Applikationen

FileReader API und FileWriter API

Page 98: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

File API: Vorarbeiten

Offline. Na und? - Strategien für offlinefähige Applikationen

var onError = function(e) {var msg = '';

switch(e.code) {case FileError.QUOTA_EXCEEDED_ERR:

msg = 'QUOTA_EXCEEDED_ERR'; break;case FileError.NOT_FOUND_ERR:

msg = 'NOT_FOUND_ERR'; break;case FileError.SECURITY_ERR:

msg = 'SECURITY_ERR'; break;case FileError.INVALID_MODIFICATION_ERR:

msg = 'INVALID_MODIFICATION_ERR'; break;case FileError.INVALID_STATE_ERR:

msg = 'INVALID_STATE_ERR'; break;default:

msg = 'Unknown Error'; break;};

alert("Error: " + msg);};

Page 99: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

File API: Vorarbeiten II

Offline. Na und? - Strategien für offlinefähige Applikationen

// File system has been prefixed as of Google Chrome 12window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;

window.BlobBuilder = window.BlobBuilder || window.WebKitBlobBuilder;

var size = 5 * 1024*1024; // 5MB

Page 100: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

File API: Quota anfordern

Offline. Na und? - Strategien für offlinefähige Applikationen

// request quota for persistent storewindow.webkitStorageInfo.requestQuota(

PERSISTENT,size,function(grantedBytes) {

window.requestFileSystem(PERSISTENT,grantedBytes,function(fs) {

// @TODO: access filesystem}

}}

}

Page 101: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 102: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

File API: Daten hinzufügen

Offline. Na und? - Strategien für offlinefähige Applikationen

function add(item) { window.webkitStorageInfo.requestQuota(

PERSISTENT, size, function(grantedBytes) {

window.requestFileSystem( PERSISTENT, grantedBytes, function(fs){

writeToFile(fs, item); }, onError

); }, function(e) {

onError(e); }

); }

Page 103: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

File API: Daten hinzufügen II

Offline. Na und? - Strategien für offlinefähige Applikationen

function writeToFile(fs, item) {fs.root.getFile(

'todo.txt',{

create: true},function(fileEntry) {

fileEntry.createWriter(function(fileWriter) {

var bb = new window.BlobBuilder();bb.append(JSON.stringify(item)+

"\n");

fileWriter.seek(fileWriter.length);fileWriter.write(

bb.getBlob('text/plain'));}, onError

);}, onError

);}

Page 104: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

File API: Daten hinzufügen II

Offline. Na und? - Strategien für offlinefähige Applikationen

function writeToFile(fs, item) {fs.root.getFile(

'todo.txt',{

create: true},function(fileEntry) {

fileEntry.createWriter(function(fileWriter) {

var bb = new window.BlobBuilder();bb.append(JSON.stringify(item)+

"\n");

fileWriter.seek(fileWriter.length);fileWriter.write(

bb.getBlob('text/plain'));}, onError

);}, onError

);}

Deprec

ated!

Page 105: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

File API: Daten hinzufügen II

Offline. Na und? - Strategien für offlinefähige Applikationen

function writeToFile(fs, item) {fs.root.getFile(

'todo.txt',{

create: true},function(fileEntry) {

fileEntry.createWriter(function(fileWriter) {

var blob = new Blob([ JSON.stringify(item)+"\n"]);

fileWriter.seek(fileWriter.length);fileWriter.write(blob);

}, onError);

}, onError);

}

Page 106: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

File API: Daten auslesen

Offline. Na und? - Strategien für offlinefähige Applikationen

function read() { window.webkitStorageInfo.requestQuota(

PERSISTENT, size, function(grantedBytes) {

window.requestFileSystem( PERSISTENT, grantedBytes, function(fs){

readFromFile(fs); }, onError

); }, function(e) {

onError(e); }

);}

Page 107: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

File API: Daten auslesen II

Offline. Na und? - Strategien für offlinefähige Applikationen

function readFromFile(fs) {fs.root.getFile(

'todo.txt',{

create: true},function(fileEntry) {

fileEntry.file(function(file){var reader = new FileReader();reader.onloadend = function(e) {

if (evt.target.readyState == FileReader.DONE) { // process this.result}

};reader.readAsText(file);

});}, onError

);}

Page 108: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Bin ich online?

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 109: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Bin ich online?

Offline. Na und? - Strategien für offlinefähige Applikationen

document.body.addEventListener("online", function () { // browser is online!}

document.body.addEventListener("offline", function () { // browser is not online!}

Page 110: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Bin ich online? Andere Vorgehensweise...

Offline. Na und? - Strategien für offlinefähige Applikationen

$.ajax({ dataType: 'json', url: 'http://myappurl.com/ping', success: function(data){ // ping worked }, error: function() { // ping failed -> Server not reachable }});

Page 111: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Datensynchronisation

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 112: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Datensynchronisation

Offline. Na und? - Strategien für offlinefähige Applikationen

PouchDB, the JavaScript Database that syncs!

Page 113: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Datensynchronisation

Offline. Na und? - Strategien für offlinefähige Applikationen

var db = new PouchDB('todo');

db.put({ _id: 1, todo: 'Get some work done...',});

db.replicate.to('http://example.com/mydb');

Page 114: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

NoBackend Bewegung

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 115: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

NoBackend Bewegung

Offline. Na und? - Strategien für offlinefähige Applikationen

Entkopplung von Frontend und Backend!

Page 116: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

NoBackend Bewegung: Hood.ie

Offline. Na und? - Strategien für offlinefähige Applikationen

„Hoodie is an architecture for

frontend-only web apps“

Page 117: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Browserunterstützung?

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 118: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Browserunterstützung?

Offline. Na und? - Strategien für offlinefähige Applikationen

App Cache Web Storage WebSQL IndexedDB File API Data URI

IE 10.0 8.0 10.0 10.0 - 8.0*

Firefox 11.0 11.0 11.0 11.0 19.0 16.0

Chrome 18.0 18.0 18.0 18.0 18.0 18.0

Safari 5.1 5.1 5.1 - - 5.1

Opera 12.1 12.1 12.1 - - 12.1

iOS Safari 3.2 3.2 3.2 - - 3.2

Android 2.1 2.1 2.1 - - 2.1

Quelle: http://caniuse.com

Page 119: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Speicherplatzbeschränkung?

Offline. Na und? - Strategien für offlinefähige Applikationen

Page 120: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Speicherplatzbeschränkung?

Offline. Na und? - Strategien für offlinefähige Applikationen

Alle vorgestellten Technologien sind durch Quotas beschränkt.

Page 121: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Speicherplatzbeschränkung?

Offline. Na und? - Strategien für offlinefähige Applikationen

App Cache Web Storage WebSQL IndexedDB File API

iOS 5.1 10 MB 5 MB 5 MB 5 MB

Android 4 unlimited 5 MB ? ?

Safari 5.2 unlimited 5 MB 5 MB 5 MB

Chrome 18 5 MB 5 MB unlimited unlimited unlimited

IE 10 50 MB 10 MB 500 MB 500 MB

Opera 11 50 MB 5 MB 5 MB 5 MB

Firefox 11 unlimited 10 MB 50 MB 50 MB

Minimumwerte, je nach Konfiguration ist mehr möglich.

Page 122: Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbstcampus13

Vielen Dank!