A simple layer over Web SQL to help avoid callback hell. It's a pretty thin layer, so you will still have to have familiarity with the WebSQL API.
The global variable WebSQLStepper
provides a single method to invoke
a transaction.
transaction(db, steps)
parameters
db
is an instance of the Web SQL Database interfacesteps
is an instance of theSteps
interface
Runs the steps in the
steps
parameter as a transaction.
A Steps
object is an object you create that provides the methods listed
below. You pass your Steps
object to WebSQLStepper.transaction()
and
then the Steps
objects' methods will be called during the lifetime of the
transaction.
All the methods invoked on a Steps
object pass the Steps
object itself
as the this
value.
A Steps
object may also have a property stepsName
, which is be used
to describe the object in diagnostics.
success()
This function will be invoked if the transaction completes successfully.
error(sqlError)
parameters
sqlError
is an instance of a Web SQL SQLError interface
This function will be invoked if the transaction does not complete successfully.
step#(transaction, sqlError, resultSet)
parameters
transaction
is an instance of aTransaction
interfacesqlError
is an instance of a Web SQL SQLError interfaceresultSet
is an instance of a Web SQL SQLResultSet interface
The
Steps
object can have multiple step methods, each named asstep1
,step2
, and so on. The steps will be executed in sequence, starting atstep1
working up to the last step. Each step must make one call to thetransaction.executeSql()
method. The results of executing that SQL statement will be passed to the next step in it's arguments - thesqlError
in case an error occurred while executing the statement, or theresultSet
object in the case the statement executed successfully. When all of the statements have completed, either thesuccess()
orerror()
method of theSteps
object will be invoked.
The transaction
parameter passed to a step is used to execute SQL
statements. It is an analogue of the Web SQL SQLTransaction
interface.
executeSql(sqlStatement, arguments)
parameters
sqlStatement
is SQL statement as a stringarguments
is an array of values to be used in the?
placeholders in thesqlStatement
WebSQLStepper.transaction database,
stepsName: 'create'
#----------------------------------
error: (sqlError) ->
store.error(sqlError, options)
#----------------------------------
step1: (tx) ->
data = JSON.stringify(model.toJSON())
tx.executeSql 'INSERT INTO store VALUES(NULL, ?)', [data]
#----------------------------------
step2: (tx, sqlError, resultSet) ->
return if sqlError
@id = resultSet.insertId
#----------------------------------
success: ->
model.id = @id
options.success model
Copyright (c) 2012 Patrick Mueller
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.