2011-09-30 164 views
10

我有下面的代码,它获取一个json记录集并在客户端Web Sql存储的三个不同表中插入一些数据。HTML5 WebSQL:如何知道db事务何时结束?

如何拦截databaseSync()函数的结尾? 我想要做的是显示一个警报或更好的ajax微调gif为了通知用户何时完成同步。

非常感谢您的帮助, ciao!

function databaseSync() { 

     // table one 
     $.getJSON("http://192.168.1.40:8888/iOS/mobilesrv/index.php?ACT=one", function(json) { 
      $.each(json.results, function(i, res) { 
       db.transaction(function(tx) { 
        tx.executeSql("INSERT INTO table1 (A, B, C, D) VALUES (?,?,?,?) ", [res.A, res.B, res.C, res.D], onSuccess, onError); 
       }); 
      }); 
     }); 

     // table two 
     $.getJSON("http://192.168.1.40:8888/iOS/mobilesrv/index.php?ACT=two", function(json) { 
      $.each(json.results, function(i, res) { 
       db.transaction(function(tx) { 
        tx.executeSql("INSERT INTO table1 (A, B, C, D) VALUES (?,?,?,?) ", [res.A, res.B, res.C, res.D], onSuccess, onError); 
       }); 
      }); 
     }); 

     // table three 
     $.getJSON("http://192.168.1.40:8888/iOS/mobilesrv/index.php?ACT=three", function(json) { 
      $.each(json.results, function(i, res) { 
       db.transaction(function(tx) { 
        tx.executeSql("INSERT INTO table1 (A, B, C, D) VALUES (?,?,?,?) ", [res.A, res.B, res.C, res.D], onSuccess, onError); 
       }); 
      }); 
     }); 


    } 
+0

+1您需要等到所有'onSuccess'或'onError'已完成调用。为任何人提供一个很好的方式来写这个。 – Thilo

回答

12

好的,这是我的第五次修订,但我喜欢这个问题,我不断想出更好的想法。这个使用jquery deferred objects,我认为它最终涵盖了所有情况,并按照它应该的方式工作。

function tableInsert(url) { 
    var dfd = $.Deferred(); 
    var arr = []; 
    $.getJSON(url, function(json) { 
     $.each(json.results, function(i, res) { 
      var dfd = $.Deferred(); 
      arr.push(dfd.promise()); 
      db.transaction(function(tx) { 
       tx.executeSql(
        "INSERT INTO table1 (A, B, C, D) VALUES (?,?,?,?) ", 
        [res.A, res.B, res.C, res.D], 
        function(){ 
         onSuccess(dfd.resolve); 
        }, 
        function(){ 
         onError(dfd.resolve); 
        } 
       ); 
      }); 
     }); 
     $.when.apply(this, arr).then(dfd.resolve); 
    }); 
    return dfd.promise(); 
} 

function databaseSync() { 

    $.when(tableInsert("http://192.168.1.40:8888/iOS/mobilesrv/index.php?ACT=one"), 
      tableInsert("http://192.168.1.40:8888/iOS/mobilesrv/index.php?ACT=two"), 
      tableInsert("http://192.168.1.40:8888/iOS/mobilesrv/index.php?ACT=three")) 
     .then(function(){ 
      console.log('All processing complete'); 
     }); 
} 

对于这个工作,你需要改变的onSuccess和onError的做其他任何这是他们做的,那么这应该为你工作后执行的决心功能作为一个回调函数。希望这个对你有帮助。

+0

我想你已经将tableInsert和insertTable混合了。 –

+0

谢谢。 –

+1

@JeffHutchins把这个交易移到每个交易之外是否有意义?我使用了一个类似于你的系统,但是当使用每个插入的事务批量插入1000行时,确实会减慢进程的速度。 – JonWells

-2

或者,你可以使用一个交易,批量插入和使用回调函数来得到通知有关交易

function doSync(){ 
    databaseSync(function(){ 
    console.log('database sync is completed') 
    }); 
} 

function databaseSync(onTrxSuccess) { 
    db.transaction(function(tx) { 
    // table one 
    $.getJSON("http://192.168.1.40:8888/iOS/mobilesrv/index.php?ACT=one", function(json) { 
     $.each(json.results, function(i, res) {     
       tx.executeSql("INSERT INTO table1 (A, B, C, D) VALUES (?,?,?,?) ", [res.A, res.B, res.C, res.D], onSuccess, onError); 
      }); 
     }); 


    // table two 
    $.getJSON("http://192.168.1.40:8888/iOS/mobilesrv/index.php?ACT=two", function(json) { 
     $.each(json.results, function(i, res) { 
       tx.executeSql("INSERT INTO table1 (A, B, C, D) VALUES (?,?,?,?) ", [res.A, res.B, res.C, res.D], onSuccess, onError); 
      }); 
    }); 

    // table three 
    $.getJSON("http://192.168.1.40:8888/iOS/mobilesrv/index.php?ACT=three", function(json) { 
     $.each(json.results, function(i, res) { 
       tx.executeSql("INSERT INTO table1 (A, B, C, D) VALUES (?,?,?,?) ", [res.A, res.B, res.C, res.D], onSuccess, onError); 
      }); 
     }); 
    }, null, onTrxSuccess); 


} 
+0

您不能保证插入将顺序发生。所以2/3可能会丢失。 – oligofren

相关问题