Created
August 5, 2011 22:42
-
-
Save ptitfred/1128705 to your computer and use it in GitHub Desktop.
HTML5 DBStorage en CoffeeScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Handlers | |
nope = (tx, obj) -> | |
displayResult = (tx, rs) -> | |
alert "Succeeded!" | |
displayError = (tx, err) -> | |
alert "Error: #{err.message}" | |
class Storage | |
constructor: (@name, @displayName) -> | |
@db = openDatabase @name, "", @displayName, 1024*1024 | |
createSchema: -> | |
@db.transaction (tx) -> | |
tx.executeSql "DROP TABLE OPTIONS", [], nope, displayError | |
tx.executeSql "CREATE TABLE OPTIONS (NAME TEXT PRIMARY KEY ASC, VALUE TEXT)", [], nope, displayError | |
insert: (name, value) -> | |
@db.transaction (tx) -> | |
tx.executeSql "INSERT INTO OPTIONS (NAME, VALUE) VALUES (?,?)", [name, value], nope, displayError | |
# Ouvre la base "coffee-db" | |
database = new Storage "coffee-db", "Coffee DB" | |
# initialise le schéma (au besoin) | |
database.createSchema() | |
# ajoute une ligne | |
database.insert "KEY1", "VALUE5" | |
database.insert "KEY2", "VALUE4" | |
database.insert "KEY3", "VALUE3" | |
database.insert "KEY4", "VALUE2" | |
database.insert "KEY5", "VALUE1" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment