2011-09-30 6 views
10

다음 코드는 json 레코드 세트를 가져오고 일부 데이터를 클라이언트 웹 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하는 사람은 좋은 방법을 써야합니다. – 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); 


} 
당신은 모든`onSuccess` 또는`onError` 때까지 기다릴 필요가
+0

삽입이 순차적으로 이루어질 것이라고 보장 할 수는 없습니다. 따라서 2/3이 손실 될 수 있습니다. – oligofren

관련 문제