0

Simply trying to write a 'selectAll' method that takes the objectStore name as a parameter and returns ALL its containing key/value pairs, rather than simply running a callback on each iteration of the object store.

In essence, mimicking synchronous behaviour when performing an indexedDB transaction.

var results = []

request.onsuccess = function(e) {

    var result = e.target.result;

    if (!result) {
        //I could call successcallback here with JUST this 'row' of data
        successCallback(result);
        return;
    }

    //I could push the results into an array here, but I would need to 'wait' until all the onSuccess methods have fired before returning it.
    results.push(result.value);
    result.continue();
}

I'm using Angular, I think promises could be the answer. In particular, could $q.all() be helpful in this instance?

1 Answer 1

1

This was actually a simpler problem than I had thought. I switched the above code to the following...

function selectAll(model, successCallback) {
    ...
    request.onsuccess = function(e) {

        var result = e.target.result;

        if (result) {
            results.push(result.value);
            result.continue();
        } else {
            $rootScope.$apply(function() {
                successCallback(results);
            });
        }
    }
}

I used a successCallback as a parameter, the calling function then returned a promise to IT'S calling function...

db.selectAll(model, function(results) {
    deferred.resolve(results);
})

Important to note the

$rootScope.$apply(function() {

around the callback call when using Angular.

1
  • An other possibility is using the oncomplete callback of the transaction object. Nov 15, 2013 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.