Files
IlLievitario-App/js/classes/cache.js

66 lines
2.6 KiB
JavaScript

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
define(['jquery', 'backbone'],
function ($, Backbone) {
var Cache = {
convertToDate: function (mysql_string)
{
if (typeof mysql_string === 'string')
{
var t = mysql_string.split(/[- :]/);
//when t[3], t[4] and t[5] are missing they defaults to zero
return new Date(t[0], t[1] - 1, t[2], t[3] || 0, t[4] || 0, t[5] || 0);
}
return null;
},
prefix: "cacheItem",
get: function (name) {
var item = window.localStorage.getItem(this.prefix + name);
if(item === null)
return null;
item = JSON.parse(item);
return item.storeValue;
},
set: function (name, value) {
window.localStorage.setItem(this.prefix + name, JSON.stringify({setTime: new Date(), storeValue: value}));
},
isExpired: function (name, timeDiff) {
var item = window.localStorage.getItem(this.prefix + name);
if(item === null)
return true;
item = JSON.parse(item);
var time = item.setTime;
if(timeDiff.getTime() > Date.parse(time))
return true;
return false;
},
checkCacheFromServer: function ()
{
var self = this;
$.ajax({
url: myManifest.Settings.DefaultURL + "/api/profile/statusCache",
//dataType: "json",
type: 'GET',
success: function (msg) {
$(msg).each(function (index, element) {
var elemName = "Category" + element.ID_CATEGORIA;
if (self.isExpired(elemName, self.convertToDate(element.LastDateModified)))
{
self.set(elemName, null);
}
});
}
});
}
};
return Cache;
});