开发者

Jquery Deferred - Is this a scope? Syntax?

开发者 https://www.devze.com 2023-03-13 18:46 出处:网络
I have a common routine for clearing out my DB: function clearDB() { // Clears the database based on code in the file \"db_definition.js\".

I have a common routine for clearing out my DB:

function clearDB() { // Clears the database based on code in the file "db_definition.js".
  开发者_Go百科  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.


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.

0

精彩评论

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

关注公众号