lundi 23 février 2015

AngularJs timestamp is null websql

I'm following this tutorial in order to get my app to sync with my API, but it is not working very well, my lastSync function is returning null, I don't know what I'm missing here, I'm having some difficult to adapt that code to work with angular.



the log:

Starting synchronization...
database.js:16 clientes table created successfully
database.js:16 Last local timestamp is null
database.js:16 undefined
database.js:16 The server returned 11 changes that occurred after null


angular.module('starter.database', [])

.factory('dbFactory', function ($q, $http, $rootScope, $ionicPlatform, ServiceClientes) {
var countries = [];
var location = "";
var deferred = $q.defer();
var dbFactory = {};
var db = window.openDatabase("Rm", "1.0", "Rm", 200000);

$ionicPlatform.ready(function () {
populateDB();
});
function log(message) {
console.log(message);
}

function populateDB() {
createTable();
sync();
}

function createTable() {
db.transaction(
function (tx) {

tx.executeSql("CREATE TABLE IF NOT EXISTS clientes (" +
"clientes_id Integer PRIMARY KEY AUTOINCREMENT, " +
"_criado Text, " +
"_modificado Text, " +
"_status Text, " +
"id_rm Integer, " +
"credencial_id Integer, " +
"informacao_adicional, " +
"nome, " +
"tipo, " +
"CONSTRAINT unique_clientes_id UNIQUE ('clientes_id'))");
tx.executeSql('CREATE INDEX IF NOT EXISTS "clientes.index_clientes_id" ON "clientes"("clientes_id");');



},
txErrorHandler,
function () {
log('clientes table created successfully');
}
);
}


function getLastSync(param) {
db.transaction(
function (tx) {
var sql = "SELECT MAX(_modificado) as lastS FROM clientes";



tx.executeSql(sql,[],
function (tx, results) {


var lastSync = results.rows.item(0).lastS;
param(lastSync);
log('Last local timestamp is ' + lastSync);

}

);

},
txErrorHandler,
function (error) {
log(error);

}
);
}

function getChanges(modifiedSince) {


ServiceClientes.getAll('clientes', modifiedSince).success(function (data) {
log("The server returned " + data.dados.length + " changes that occurred after " + modifiedSince);
}).error(function (error) {
console.log(error);
});


}

function applyChanges() {

angular.forEach(data.dados, function (item) {
db.transaction(
function (tx) {
log(item.nome + " : " + item.tipo_pessoa);

tx.executeSql('INSERT OR REPLACE INTO clientes (nome, tipo, ' +
'_criado,' +
'_modificado , ' +
'_status, ' +
'id_rm, ' +
'informacao_adicional ) VALUES (?,?,?,?,?,?,?)',
[item.nome, item.tipo_pessoa, item.criado, item.modificado, item.status, item.id, item.informacoes_adicionais]);

},
txErrorHandler,
function (tx) {
}
);
});
}

function sync() {

log('Starting synchronization...');

getLastSync(function (lastSync) {


getChanges(lastSync, function (changes) {

log('awpdwada ' + changes);
if (changes.length > 0) {
applyChanges(changes);
} else {
console.log('Nothing to synchronize');
}

}
);
});

}




function txErrorHandler(tx) {
log(tx.message);
}


My service:



angular.module('starter.services', [])

.factory('ServiceClientes', ['$http', function ($http) {


var endpoint = 'http://api.re.co/api/';
var token = '99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G90bDAcbFfd19MfacGf3FFm8CM1hG0eDiIk8';
var credencial = 'rm@w.com:cd87cd5ef753a06ee775dc7cfe66c';
var origem = 'mobile';


var config = {
dataType: 'json',
method: 'GET',
headers: {
'X-API-TOKEN': token,
'X-API-CREDENCIAL': credencial,
'X-API-ORIGEM': origem,
"Content-Type": "application/json"
}
};


return {
getAll: function (url, modificado ) {
config.url = endpoint + url;
config.data = modificado;
return $http(config);
}
};



}]);

Aucun commentaire:

Enregistrer un commentaire