Hi David,
As per your programming is best way but I have done with another way using web worker .
I have used a Web Worker create ajax post request pass data to server DB from localDB match Data with linq and finally get the updated Record
through JSON format then Updating in local DB
I have done like this , tell me this is good way or not , suggest me best way
this is my Worker.js
var i = 0;
onmessage = function (e) {
if (e.data[0] === "start") {
done()
}
else {
//console.log(e.data)
debugger;
doCall(e.data[0],e.data[1]);
}
};
//sending information to server
function doCall(data,url) {
try {
var resourceData = JSON.stringify(data);
var requestUrl = url;
var client = null;
if (XMLHttpRequest) {
client = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
client = new ActiveXObject("Microsoft.XMLHTTP");
}
client.onreadystatechange = function () {
if (client.readyState == 4 && client.status == 200) {
var myArr = JSON.parse(client.responseText);
callBackreturn(myArr);
}
};
client.open("POST", requestUrl, true);
client.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
client.send('jsonData=' + resourceData)
} catch (ex) {
}
}
//waiting and processing server response
function callBackreturn(response) {
try {
postMessage(response);
}
catch (ex) {
}
}
Index.chtml page
java script function
setInterval(init, 50000)
function init() {
debugger;
var requestUrl = '@Url.Action("getRsourceUpdatedRecords")';
if (typeof (Worker) === undefined) {
alert('No webworker supported');
return false;
}
myWorker = new Worker("../Scripts/worker.js");
var resourcedata = fetchlocalResourceRecord();
resourcedata.then(function (a) {
myWorker.postMessage([a,requestUrl]);
});
myWorker.onmessage = function (e) {
updateRecord(e.data);
};
}
function updateRecord(data)
{
debugger;
db.transaction("rw", db.resourceTbl, function () {
jQuery.each(data, function (index, value) {
db.resourceTbl.where("ResourceId").equals(this.ResourceId).modify({ EmployeeCode: this.EmployeeCode, DisplayName: this.DisplayName
, AdrenalinResourceStatus: this.AdrenalinResourceStatus, DateOfJoining: this.DateOfJoining
})
});
//db.friends.where("shoeSize").aboveOrEqual(47).modify({isBigfoot: 1});
}).catch(Dexie.ModifyError, function (e) {
console.error(e.failures.length + " items failed to modify");
}).catch(function (e) {
console.error("Generic error: " + e);
});
}