开发者

Pass variable to callback

开发者 https://www.devze.com 2023-04-03 12:55 出处:网络
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'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?


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 = ->) ->
  # ...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号