Created
April 3, 2013 21:41
-
-
Save johnnyt/5305650 to your computer and use it in GitHub Desktop.
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
Smalltalk current createPackage: 'HTML5' properties: #{}! | |
Object subclass: #LocalStorage | |
instanceVariableNames: 'scope' | |
category: 'HTML5'! | |
!LocalStorage commentStamp! | |
LocalStorage is a wrapper around HTML5 Local Storage. | |
Take a look at: http://diveintohtml5.org/storage.html | |
Example | |
| local | | |
local := LocalStorage new. | |
local at: 'message' put: 'Hello World!!'. | |
Transcript show: (local at: 'message'); cr.! | |
!LocalStorage methodsFor: 'accessing'! | |
keys | |
| keysArray | | |
keysArray := #(). | |
0 to: (localStorage length - 1) do: [ :idx :key | | |
key := localStorage key: idx. | |
(key match: '^', scope) | |
ifTrue: [ keysArray add: (key replace: ('^', scope) with: '') ]]. | |
^keysArray | |
! | |
scopedKey: aString | |
(aString match: '^' , scope) | |
ifTrue: [ ^aString ] | |
ifFalse: [ ^scope , aString ] | |
! | |
values | |
^self keys collect: [ :each | self at: each ] | |
! | |
at: aString | |
^localStorage getItem: (self scopedKey: aString) | |
! | |
at: aString put: anObject | |
localStorage | |
setItem: (self scopedKey: aString) | |
value: anObject | |
! | |
delete: aString | |
localStorage removeItem: (self scopedKey: aString) | |
! | |
includesKey: aString | |
^self keys includes: aString | |
! | |
at: aKey ifAbsent: aBlock | |
^(self includesKey: aKey) | |
ifTrue: [ self at: aKey ] | |
ifFalse: aBlock | |
! ! | |
!LocalStorage methodsFor: 'initialization'! | |
initialize | |
scope ifNil: [ scope := '' ] | |
! | |
initializeWithScope: aString | |
scope := aString , '.'. | |
self initialize. | |
! ! | |
LocalStorage class instanceVariableNames: 'root'! | |
!LocalStorage class methodsFor: 'instance creation'! | |
withScope: aString | |
^self new initializeWithScope: aString | |
! ! | |
Object subclass: #WebDatabase | |
instanceVariableNames: 'name description request db version' | |
category: 'HTML5'! | |
!WebDatabase commentStamp! | |
HTML5 Web SQL Database | |
http://www.html5rocks.com/en/features/storage! | |
!WebDatabase methodsFor: 'accessing'! | |
db | |
^db | |
! | |
name: aString | |
name := aString. | |
! | |
name | |
^name | |
! | |
description | |
^description | |
! | |
description: aString | |
description := aString | |
! | |
version: aString | |
version := aString. | |
! | |
version | |
^version | |
! ! | |
!WebDatabase methodsFor: 'api'! | |
executeSql: aSqlString args: anArray onSuccess: aSuccessBlock onError: anErrorBlock | |
db transaction: [ :tx | | |
tx executeSql: aSqlString | |
args: anArray | |
onSuccess: aSuccessBlock | |
onError: anErrorBlock ] | |
! | |
executeSql: aSqlString args: anArray | |
self executeSql: aSqlString | |
args: anArray | |
onSuccess: [] | |
onError: [ :tx :error | console log: error ] | |
! | |
executeSql: aSqlString | |
self executeSql: aSqlString | |
args: [] | |
onSuccess: [] | |
onError: [ :tx :error | console log: error ] | |
! ! | |
!WebDatabase methodsFor: 'initialization'! | |
initialize | |
name ifNil: [ name := 'default' ]. | |
description ifNil: [ description := 'Default DB' ]. | |
version := ''. | |
db := window openDatabase: name version: version description: description size: 1024. | |
! ! | |
!WebDatabase class methodsFor: 'instance creation'! | |
named: aName description: aDescription | |
^self basicNew | |
name: aName; | |
description: aDescription; | |
initialize; | |
yourself. | |
! | |
named: aName | |
^self basicNew | |
name: aName; | |
description: ''; | |
initialize; | |
yourself. | |
! ! | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment