0

I want to build up on an existing solution to find out when all elements in an array have been inserted successfully.

Web SQL Database + Javascript loop

insert: function(configurationVOs) {
    var self = this;

    this.database.transaction(function(transaction){
        var total = 0;
        for(var i=0; i<configurationVOs.length; i++) {
            (function(configurationVO) {
                var params = [configurationVO.id, configurationVO.key, configurationVO.value, configurationVO.type];
                transaction.executeSql(self.configurationInsertSQL, params, self.insertComplete, self.insertError);
            })(configurationVOs[i]);
        }
    });
},

if someone can provide an elegant self-contained solution within this for loop/function that can do a single callback upon success.

1 Answer 1

0

My attempt, please edit/suggest for any improvements.

insert: function(configurationVOs) {
        var self = this;
        this.database.transaction(function(transaction){
            var total = 0;
            for(var i=0; i<configurationVOs.length; i++) {
                (function() {
                    var params = [configurationVOs[i].id, configurationVOs[i].key, configurationVOs[i].value, configurationVOs[i].type];
                    transaction.executeSql(self.configurationInsertSQL, params, function(transaction, resultSet){
                        if(++total == configurationVOs.length) {
                            self.insertComplete();
                        }
                    }, self.insertError);
                })();
            }
        });
    },
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.