Javascript 将 SQL 结果绑定到函数

发布于 2024-11-19 09:19:44 字数 1196 浏览 3 评论 0原文

所有,

我正在使用 WebOS enyo,但有一个非常高级的时刻...

基本上,它的 enyo 与我的问题无关,但是...

我有一个方法:

clickPopulate: function(){

     // Do some SQL

};

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

 * 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 回调。

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,但作为它的调用方法,我无法让它工作?

有什么想法吗?

All,

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

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

I have a method:

clickPopulate: function(){

     // Do some SQL

};

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)

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)});

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?

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

女皇必胜 2024-11-26 09:19:44

您无法让异步回调返回原始调用者。

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

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

所以基本上你可以在你的函数中包含一个闭包作为回调。这样,它看起来像是原始调用者的一部分,但实际上不是。


如果您真的愿意,您可能可以让它调用原始函数,并定义一些参数来确定它是否是查询的结果,但这只会使它变得有点丑陋和混乱。

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文