0

Is this possible easily? It seems the handleResult method is only executed if the result isn't the empty set.

A thought I had was to have handleResult and handleCompletion be member functions of an object and have handleResult update a member variable that handleCompletion can check. If the variable is set, not empty, if variable unset, empty and can act accordingly.

seems to be overly complicated and hoping there's a better solution?

Arithmomaniac
  • 4,604
  • 3
  • 38
  • 58
spotter
  • 1,178
  • 1
  • 9
  • 19

1 Answers1

1

to sketch out a solution (the thought i had above) (edit2: per comment I made below)

function sql() {
    this.results = false;
    var me = this;

    this.handleResult = function(aResultSet) {

        for (var row = aResultSet.getNextRow(); row;  row = aResultSet.getNextRow()) {
            me.results = true;

            var value = row.getResultByName("name");
        }
    };

    this.handleError = function(aError) {
        .... //deal with error
    };

    this.handleCompletion = function(aReason) {
        if (me.results) {
            ....//results
        } else {
            ....//no results
        }

        if (aReason != Components.interfaces.mozIStorageStatementCallback.REASON_FINISHED) {
            ....//handle these
    };
};

s = new sql(); 

statement.executeAsync({
    handleResult: s.handleResult,
    handleError: s.handleError,
    handleCompletion: s.handleCompletion
});

is this considered a good way to solve this problem?

edit1: this doesn't behave in the manner I'd expect (it works, but not 100% sure why). i.e. the this.results variable is undefined (not false), if handleResult never runs. So it appers as if handleResult and handleCompletion are operating on a different set of variables than I'd expect.

any help to understand what I'm doing wrong would be appreciated.

spotter
  • 1,178
  • 1
  • 9
  • 19
  • http://w3future.com/html/stories/callbacks.xml is the answer to my issue. need a var me = this; in the sql object and reference internal variables by "me." instead of "this.". – spotter Jul 03 '12 at 05:13