开发者

How to set status with HTML5 Web Database api

I'm using the HTML5 Web Database API and I have a function that checks to see if the app needs to go and perform it's setup phase :

this.setupRequired = function() {
    var status;
    try {
        this.db.transaction(function(tx) {
            tx.executeSql("SELECT * FROM settings", [], function (tx,result) {
                if (result.rows.length == 0) {
                    status =  true;
                } else {
                  status = false;
                }
            }, function(tx) {
                status = true;
            })
        });
    } catch (e) {
        return true;
    }
    return status
}

I'd like to return true or false based on开发者_如何转开发 whether there is data in the settings table or not (or if the settings table doesn't exist). The status var isn't getting set, I'm guessing this is due to scope issues and the anonymous callback functions. I'm pretty sure I need to use a closure here to correct the issue but can't quite get it right.


I'd like to return true or false based on whether there is data in the settings table or not

You can't. It is not known whether there is data in the settings table at the time the setupRequired() method has to return. This will only be known when the SQL database has performed the query and invoked the callback function. That happens only after setupRequired() and the functions that led to it being called have all exited, returning control to the browser.

That's the whole point of the callback function being passed to executeSql(). The function is not executed right away, so status will not have been touched by the time return status is reached. What's more, any exceptions that occur inside the callback functions will not cause the catch(e) block to be executed, because the try...catch block will long have been exited by the time the function that was defined inside it is actually called. So this try...catch is effectively useless.

This is ‘asynchronous’ coding. Just because some code is below a function, doesn't mean the function's going to execute first.


You could probably set status as a property of the object or another object of your own making.

You could also check at each stage what 'this' is or what value status has using firebug.


Hmm, I don't have any experience using this API but it appears that you are trying to set a value in a callback function and then return that value from the outer function. Assuming the callback is asynchronous, this will not work. I think your best bet is to change your code to something like:

this.setupRequired = function() {
    try {
        this.db.transaction(function(tx) {
            tx.executeSql("SELECT * FROM settings", [], function (tx,result) {
                if (result.rows.length == 0) {
                    doYourSetupFunction();
                } else {
                  //don't;
                }
            }, function(tx) {
                doYourSetupFunction();
            })
        });
    } catch (e) {
        doYourSetupFunction();
    }
}

So that the setupRequired function is reponsible for checking and triggering the setup.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜