!!! Listings zUum Artikel "App ohne Store" !!! von Stefan Mintert in iX 1/2011, S. 134 !!! Listing 1: Auf iPhone abgestimmtes HTML Aivee

Reload

!!! Listing 2: JavaScript-Funktion today() !!! Listing 3: Zwei neue Steuerelemente Merken Gemerkte Texte !!! Listing 4: Initialisierung der Datenbank Aivee = {}; Aivee.config = { db: { name: "AiveeBookmarks", version: "1.0", displayName: "Aivee-Lesezeichen", estSize: 30000 } } function init() { Aivee.DB = initDB(Aivee.config.db,createAiveeTables); today(); } !!! Listing 5: Datenbank öffnen function initDB(dbConfig, createTablesFunction) { try { if (!window.openDatabase) { alert("FEHLER: Dein Browser unterstützt keine Javascript-Datenbanken."); return false; } else { var myDB = openDatabase(dbConfig.name, dbConfig.version, dbConfig.displayName, dbConfig.estSize); createTablesFunction(myDB); return myDB } } catch (e) { alert("FEHLER: Fehler " + e); return false; } } !!! Listing 6: Tabellen anlegen function createAiveeTables(aiveeDB) { aiveeDB.transaction ( function (tx) { tx.executeSql('CREATE TABLE IF NOT EXISTS AiveeBookmarks (BookmarkNr INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, Message TEXT NOT NULL, Section TEXT NOT NULL, MessageId INTEGER NOT NULL);', [], nullDataHandler,errorHandler); } ) } !!! Listing 7: showMessage() function showMessage(jsonObject) { var envelope = eval('(' + jsonObject + ')'); document.getElementById("message").innerHTML = envelope.message; document.getElementById("section").innerHTML = envelope.section; document.getElementById("bookmarkContainer").innerHTML = ""; document.getElementById("setBookmarkControl").onclick = function () { insertBookmarkIntoDB(envelope.message, envelope.section, envelope.messageId); return false; } } !!! Listing 8: insertBookmarkIntoDB() function insertBookmarkIntoDB(message, section, mId) { if (Aivee.DB) { var successHandler = function () { alert("Lesezeichen gespeichert."); } Aivee.DB.transaction( function (tx) { tx.executeSql('INSERT INTO AiveeBookmarks (BookmarkNr, Message, Section, MessageId) VALUES (NULL, "' + escapeQuotes(message) + '", "' + escapeQuotes(section) + '", "' + mId + '")', [], successHandler, errorHandler); } ); } else { alert("FEHLER: Datenbank wurde nicht initialisiert.\nSpeicherung von Lesezeichen nicht möglich."); } } !!! Listing 9: showBookmarks() function getAiveeBookmarks() { if (Aivee.DB) { var showBookmarks = function (tx, results) { var bookmarks = new Object(); for (var i = 0; i < results.rows.length; i++) { var row = results.rows.item(i); var section = unescapeQuotes(row.Section); var message = unescapeQuotes(row.Message); if ( ! bookmarks[section] ) { bookmarks[section] = new Array(); } bookmarks[section].push(message); } var htmlOutput = ""; for (var section in bookmarks) { htmlOutput += "

" + section + "

"; for (var i = 0; i < bookmarks[section].length; i++) { htmlOutput += "

" + bookmarks[section][i] + "

"; } } document.getElementById("section").innerHTML = ""; document.getElementById("message").innerHTML = ""; document.getElementById("bookmarkContainer").innerHTML = htmlOutput; } Aivee.DB.transaction ( function (tx) { tx.executeSql('SELECT * FROM AiveeBookmarks ORDER BY Section;', [], showBookmarks, errorHandler); } ) } else { alert("FEHLER: Datenbank nicht initialisiert. Kann Lesezeichen nicht laden."); } } !!! Listing 10: Anführungszeichen vereinheitlichen function escapeQuotes(text) { return String(text).replace(/(['"])/g,"$1$1"); } function unescapeQuotes(text) { return String(text).replace(/(')'/g,"$1").replace(/(" )"/g,"$1"); } !!! Listing 11: Geoposition ermitteln