Jquery Deferred - 这是一个范围吗?句法?

发布于 2024-11-16 00:36:59 字数 1661 浏览 5 评论 0原文

我有一个清除数据库的通用例程:

function clearDB() { // Clears the database based on code in the file "db_definition.js".
    var dwdb_dfd = $.Deferred();
    var idx = 0;

    this.maxDeletes = g_sSQL_dropWorkoutDB.length;
    this.successCount = 0;
    parentThis = this;
    do {
        $.when(transactDB(g_sSQL_dropDB[idx])).done(function() {
            if(parentThis.successCount++ >= parentThis.maxDeletes) dwdb_dfd.resolve();
        });
    } while (++idx < this.maxDeletes);
    return dwdb_dfd.promise();
}

这个例程是从以下位置调用的:

this.getNextDataSet = function() {
    $.when(clearDB()).done(function() {
    window.location.href = 'http://'+ document.location.host + '/webDBBuilder.php'
}

WebDBBuilder.php 是一个从我们的服务器读取数据并构建/重建本地 WebDB 的文件(尽管有 W3C,该死的)早在他们抛弃我们之前我们就开始了这一工作)来自服务器数据库上的可用数据。当轮询服务器的例程标记新数据可用时,调用此例程。然后,我们销毁本地数据库中的某些表——全局变量g_sSQL_dropDB只是一个DROP TABLE IF EXISTS tbl_someTable字符串数组。

transactDB() 例程是另一个延迟轴承函数,它执行传递给它的 SQL 语句。 transactDB() 正在按要求工作,因为相关表已被正确删除,并且它在应用程序的其余部分中使用。 getNextDataSet() 函数是较大对象的一部分。它被正确调用。

问题是页面跳转根本不会发生。这是 clearDB() 函数中的 Deferred 对象的某种范围问题吗?

我的代码中是否缺少某些内容?我尝试过使用done()、resolve()、$.when().then()。没什么。

任何帮助将不胜感激。我在 jQuery 论坛上问了几个问题,但基本上没有得到回应,所以我想我在 stackoverflow 上会运气更好!

<------------ 已添加 -------------->

感谢您的宝贵意见。我采用了 Julian 给我的代码(通过 $.map 进行了小语法切换),它运行得非常好!

您对函数中“this”引用的看法是正确的。我之前从一堂课中取出了一些代码,只是脑子里放屁了。 var“g_sSQL_dropWorkoutDB”确实是一个全局变量,位于包含的文件中,该文件仅包含用于构造和删除数据库的变量。

朱利安,非常感谢!

斯科特.

I have a common routine for clearing out my DB:

function clearDB() { // Clears the database based on code in the file "db_definition.js".
    var dwdb_dfd = $.Deferred();
    var idx = 0;

    this.maxDeletes = g_sSQL_dropWorkoutDB.length;
    this.successCount = 0;
    parentThis = this;
    do {
        $.when(transactDB(g_sSQL_dropDB[idx])).done(function() {
            if(parentThis.successCount++ >= parentThis.maxDeletes) dwdb_dfd.resolve();
        });
    } while (++idx < this.maxDeletes);
    return dwdb_dfd.promise();
}

And this routine is called from:

this.getNextDataSet = function() {
    $.when(clearDB()).done(function() {
    window.location.href = 'http://'+ document.location.host + '/webDBBuilder.php'
}

WebDBBuilder.php is a file that reads data from our server and builds/rebuilds the local WebDB (despite the W3C, dammit. We started this long before they abandoned us) from the data available on the server DB. This routine is called when the routine polling the server flags that new data is available. We then destroy certain tables in the local DB -- the global var g_sSQL_dropDB is simply an array of DROP TABLE IF EXISTS tbl_someTable strings.

The transactDB() routine is yet another deferred-bearing-function that executes a SQL statement passed to it. The transactDB() is working as required as the tables in question are being deleted properly and it is used throughout the rest of the app. The getNextDataSet() function is part of a larger object. It is being called correctly.

The problem is that the page jump simply never happens. Is this some sort of scope problem with the Deferred object in the clearDB() function?

Is there something in the code I am missing? I've tried using done(), resolve(), $.when().then(). Nada.

Any help would be deeply appreciated. I've asked a couple of questions in the jQuery forum with basically no responses, so I figured I'd have better luck here with stackoverflow!

<------------ Added -------------->

Thanks for the great input. I took the code Julian gave me and (with a minor syntax switch of $.map), it worked beautifully!

And you were right about the "this" reference in the function. I had pulled some code out of a class previously and simply had a brain fart. The var "g_sSQL_dropWorkoutDB" is indeed a global that sits in an included file that holds only the vars used to construct and remove the database.

Julian, thanks a bunch!

Scott.

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

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

发布评论

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

评论(1

滿滿的愛 2024-11-23 00:36:59

首先,您可以大大简化代码:

function clearDB() {
    return $.when.apply( $, $.map( g_sSQL_dropWorkoutDB, function( _, item ) {
        return transactDB( item );
    }) ).fail(function() {
        console.log( arguments );
    });
}

使用 $.when.apply,您可以将对象数组传递给 $.when,就好像它们是它的参数一样。我使用 $.map 将项目列表转换为 transactDB 对象。显然,我还没有测试过代码,但它应该可以工作。

其次,在clearDB中将变量分配给“this”的方式非常可疑。当您像在 getNextDataSet 中那样调用 clearDB 时,“this”是窗口对象,而 maxDeletes & 是窗口对象。 successCount 被分配为流程中的全局变量。不确定这是否是您的意图,而且绝对不安全。

另外,g_sSQL_dropWorkoutDB 不作为参数传递给clearDB 是否正常?全局变量还是拼写错误?

最后,我添加了一个失败回调并记录了参数。这样,您可以在控制台中检查您的一个 transactDB 调用是否实际上没有失败。

First of all, you can simplify your code greatly:

function clearDB() {
    return $.when.apply( $, $.map( g_sSQL_dropWorkoutDB, function( _, item ) {
        return transactDB( item );
    }) ).fail(function() {
        console.log( arguments );
    });
}

Using $.when.apply, you can pass an array of objects to $.when as if they were its arguments. I use $.map to transform the list of items into transactDB objects. I haven't tested the code, obviously, but it should work.

Secondly, the way you assign variables to "this" in clearDB is quite suspicious. When you call clearDB like you do in getNextDataSet, "this" is the window object and maxDeletes & successCount are assigned as global variables in the process. Not sure it's what you intended and it's definitely not safe.

Also, is it normal that g_sSQL_dropWorkoutDB is not passed to clearDB as an argument? Global variable or typo?

Finally, I added a fail callback and I log the arguments. That way, you can check in the console if one of your transactDB calls does not, actually, fail.

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