2011-09-30 11 views
10

Bir json kayıt kümesine sahip olan ve istemci Web Sql depolama biriminde üç farklı tabloya bazı veriler ekleyen aşağıdaki kodu kullanıyorum.HTML5 WebSQL: bir db işlemi bittiğinde nasıl bilinir?

databaseSync() işlevinin sonunu nasıl engelleyebilirim? Yapmak istediklerim, senkronizasyon tamamlandığında kullanıcıyı bilgilendirmek için bir uyarı veya daha iyi bir ajax çevirici gif göstermektir.

Yardımlarınız için çok teşekkürler, 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 olmuştur işleminin tamamlanmasından sonraki Hakkında bildirim almak aradı. Bunu yazmanın güzel bir yolu olan herkes için +1. – Thilo

cevap

12

Tamam, bu benim beşinci gözden geçirmendir, ancak bu soruyu beğendim ve daha iyi fikirler bulmaya devam ediyorum. Bu, jquery deferred objects'u kullanır ve sonunda tüm vakaları kapsar ve gerektiği gibi çalışır. Bunun için

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'); 
     }); 
} 

bunu yapmaları ve daha sonra bu sizin için çalışması gerekir başka ne olursa olsun yaptıktan sonra bir geri arama fonksiyonu olarak çözmek işlevini yürütmek için onSuccess ve OnError değiştirmeniz gerekir çalışmak. Umarım bunu faydalı bulursun.

+0

Bence tableInsert ile insertTable'ı karıştırdınız. –

+0

Bunun için teşekkürler. –

+1

@JeffHutchins İşlemi her birinin dışına taşımak mantıklı olmaz mıydı? Sizinkilere benzeyen bir sistem kullandım, ancak her satır için bir işlemi kullanarak 1000 satırlık toplu bir işlem eklemek işlemi – JonWells

-2

Alternatif olarak, toplu ekleme ve kullanım geri arama işlevi için bir işlem kullanabilirsiniz

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); 


} 
Hepiniz `onSuccess` veya` onError` kadar beklemek gerekir
+0

Eklerin ardışık olarak gerçekleşeceği garanti edilmez. Yani 2/3 kaybolabilir. – oligofren

İlgili konular