将变量传递给回调
我正在构建一个具有离线功能的应用程序,并正在使用 WebSQL(我知道它已被弃用,但它是 PhoneGap 附带的)
我想创建一个 SQL 查找函数来解析结果,然后调用我传递给的函数findAll
函数。 这是coffeescript,但我可以翻译成Javascript,如果这能给我答案的话!
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
如何在 findAll
函数中指定 querySuccess
函数的回调?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使用中间回调,而不是直接进入
querySuccess
,使用=>
来保留@db
的上下文:这将允许转发传递给
findAll
的callback
:然后调整参数的
querySuccess
:You could try using an intermediate callback rather than going directly to
querySuccess
, with=>
to keep context for@db
:This will allow it to forward on the
callback
passed tofindAll
:Then adjust
querySuccess
for the argument: