2011-09-30 163 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

相關問題