Question

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.

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top