git-svn-id: https://msi/svn/firstRepo/WebClient/branches/Manut_2.2.3@25 0f545695-f87b-41b6-9a03-7f16563b5454

This commit is contained in:
2015-01-25 13:57:37 +00:00
parent 1b92a374cc
commit d0d55ee8e5
19 changed files with 517 additions and 292 deletions
+66
View File
@@ -0,0 +1,66 @@
/*
* 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;
});
+54
View File
@@ -0,0 +1,54 @@
/*
* 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', 'underscore', 'backbone', 'classes/cache'],
function ($, _, Backbone, Cache) {
var CachedCollection = Backbone.Collection.extend({
cacheName: null,
fetch: function (options) {
options = _.defaults(options || {}, {parse: true});
var deferred = new $.Deferred(),
self = this;
var cachedItem = Cache.get(this.cacheName);
if (cachedItem !== null)
{
window.setTimeout(function () {
self[options.reset ? 'reset' : 'set'](false, options);
deferred.resolve(cachedItem);
self.push(cachedItem);
self.trigger('sync', self, false, options);
if (_.isFunction(options.success)) {
options.success(self, false, options);
}
return deferred;
}, 0);
}
else
{
// Delegate to the actual fetch method and store the attributes in the cache
var jqXHR = Backbone.Collection.prototype.fetch.apply(this, arguments);
// resolve the returned promise when the AJAX call completes
jqXHR.done(_.bind(deferred.resolve, this, this))
// Set the new data in the cache
.done(_.bind(
function (a, b, c) {
Cache.set(self.cacheName, a);
}
, null, this, options))
// Reject the promise on fail
.fail(_.bind(deferred.reject, this, this));
deferred.abort = jqXHR.abort;
// return a promise which provides the same methods as a jqXHR object
return deferred;
}
}
});
return CachedCollection;
});
+2 -2
View File
@@ -73,12 +73,12 @@ define(['jquery', 'backbone'],
},
isGPlusConnected: function(){
if(this.data.type == "gplus")
if(this.data.type === "google")
return true;
return false;
},
isFbConnected: function(){
if(this.data.type == "fb")
if(this.data.type === "facebook")
return true;
return false;
},