Advertisement
simi26j

javascript - PhoneGap database sync part 2

Aug 5th, 2013
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. *   this code is based on Raymond Camdens blog post:
  3. *   Adding database synchronization to your PhoneGap project
  4. *   http://bit.ly/14tXNm4
  5. *   requires the json2.js library (parser)
  6. */
  7.  
  8. function uploadFromDevice() {
  9.     var newRows = new Array();
  10.     var date = localStorage["lastdate"]?localStorage["lastdate"]:'';
  11.     if (date == '') date = '2000-01-01 12:00:00'; // really long ago
  12.     console.log('checking if there are any rows modifed/inserted after '+ date);
  13.     db.transaction(function(ctx) {
  14.         ctx.executeSql("select name,description,LastModDate,deleted,token " +
  15.             "from docs where LastModDate > ?", [date], function(tx,checkres) {
  16.             if(checkres.rows.length) {
  17.                 console.log('found ' + checkres.rows.length + ' rows that need to be uploaded to the server!');
  18.                 for(var i=0; i<checkres.rows.length; i++) {
  19.                     uRows.push(checkres.rows.item(i));
  20.                 }
  21.                 json = JSON.stringify(uRows);
  22.                 $.get(uploadUrl, {rows:json}, function(response, code){
  23.                     console.log(response);
  24.                 });
  25.             }
  26.         });
  27.     });
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement