Question

I'm building an app with offline functionality and am working with with WebSQL (I know it's deprecated, but it's what comes with PhoneGap)

I want to create an SQL find function that parses results and then calls a function that I'm passing to the findAll function. This is coffeescript, but I can translate into Javascript if that will get me an answer!

class window.TimeTravelDB

  findAll: (tableName, callback) ->
    @db.transaction (tx) ->
      tx.executeSql("Select * from #{tableName}", [], @db.querySuccess, @db.onError)

  querySuccess: (tx, results) ->
    rows = results.rows
    results = (JSON.parse(rows.item(i).data) for i in [0...rows.length])
    callback(results)
    return @results

How can I specify the callback for the querySuccess function in the findAll function?

Was it helpful?

Solution

You could try using an intermediate callback rather than going directly to querySuccess, with => to keep context for @db:

(tx, results) => @db.querySuccess(tx, results, callback)

This will allow it to forward on the callback passed to findAll:

findAll: (tableName, callback) ->
  @db.transaction (tx) ->
    tx.executeSql("Select * from #{tableName}", [],
      (tx, results) => @db.querySuccess(tx, results, callback),
      @db.onError
    )

Then adjust querySuccess for the argument:

querySuccess: (tx, results, callback = ->) ->
  # ...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top