Javascript将SQL结果绑定到函数 [英] Javascript Binding SQL results to a function

查看:96
本文介绍了Javascript将SQL结果绑定到函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

我正在使用WebOS的enyo,但是有一个非常重要的时刻....

I am working with WebOS enyo, but having a really senior moment....

基本上,这个事实与我的问题无关...

Basically the fact its enyo has no relation to my question however...

我有一个方法:

clickPopulate: function(){

     // Do some SQL

};

我正在使用数据库类来处理我的SQL lite Db连接,我正在使用的方法的接口是:

I am using a database class to handle my SQL lite Db connection, the interface for the method i am using is:

 * Execute an arbitrary SQL command on the database.
 *
 * If you need to execute multiple commands in a transaction, use queries()
 *
 * Parameters:
 * - sql (string or query object, required)
 * - options (object):
 *    * values (array): replacements for '?' placeholders in SQL
 *      (only use if not passing a DatabaseQuery object)
 *    * onSuccess (function): method to call on successful query
 *        + receives single argument: results as an array of objects
 *    * onError (function): method to call on error; defaults to logging
 */


query: function(sql, options)

所以无论如何,我都会向它发送一些SQL和一些选项,其中之一是onSuccess回调.

So anyway i send it some SQL and some options, one of which is the onSuccess callback.

this.$.db.query("SELECT fullName, count(*) FROM user WHERE username=? and password=? GROUP BY username",
        {values: [inUser,inPass], onSuccess: enyo.bind(this, this.callBackFunction)});

我真正想做的是将SQL结果数组返回到我的单击处理程序函数-clickPopulate,但是作为调用方法,我无法使其工作?

What i really want to be able to do is have the SQL result array return to my click handler function - clickPopulate, but as its the calling method, i cant get it to work?

有什么想法吗?

推荐答案

您不能让异步回调返回原始调用者.

You can't have an asynchronous callback return to the original caller.

您能做的最接近的事情是这样的(因为我不知道Enyo api,所以我只会使用一些伪的东西)

The closest you can do is something like this (since I don't know the Enyo apis I'll just use some pseudo'ish stuff)

function clickPopulate() {
    db.query('Some SQL here', function(results) {
        //This is the code that will be run once the query is complete.
    });
}

因此,基本上,您可以在函数内部包含一个闭包作为回调.这样,它看起来像是原始呼叫者的一部分,但实际上不是.

So basically you can include a closure as the callback inside your function. This way it kind of looks like it's part of the original caller but it really isn't.

如果您确实希望这样做,则可以让它回调原始函数,并定义一些参数来确定它是否是查询的结果,但这只会使它有点丑陋和混乱.

If you really wanted to, you could probably have it call the original function back, and define some parameter which is used to determine whether it's the results from the query, but that would just make it kinda ugly and confusing.

这篇关于Javascript将SQL结果绑定到函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆