1
0
mirror of https://github.com/owncloudarchive/contacts.git synced 2024-11-28 10:24:11 +01:00

Add localStorage test. Fix #453

This commit is contained in:
Thomas Tanghus 2014-04-01 03:38:49 +02:00
parent 06f865beeb
commit f1801deb79

View File

@ -57,6 +57,28 @@ OC.Contacts = OC.Contacts || {};
this.user = user ? user : OC.currentUser; this.user = user ? user : OC.currentUser;
}; };
/**
* Test if localStorage is working
*
* @return bool
*/
Storage.prototype.hasLocalStorage = function(jqXHR) {
if (Modernizr) {
if (!Modernizr.localStorage) {
return false;
}
}
// Some browsers report support but doesn't have it
// e.g. Safari in private browsing mode.
try {
OC.localStorage.setItem('Hello', 'World');
OC.localStorage.removeItem('Hello');
} catch (e) {
return false;
}
return true;
};
/** /**
* When the response isn't returned from requestRoute(), you can * When the response isn't returned from requestRoute(), you can
* wrap it in a JSONResponse so that it's parsable by other objects. * wrap it in a JSONResponse so that it's parsable by other objects.
@ -148,7 +170,7 @@ OC.Contacts = OC.Contacts || {};
Storage.prototype.deleteAddressBook = function(backend, addressBookId) { Storage.prototype.deleteAddressBook = function(backend, addressBookId) {
var key = 'contacts::' + backend + '::' + addressBookId; var key = 'contacts::' + backend + '::' + addressBookId;
if(OC.localStorage.hasItem(key)) { if(this.hasLocalStorage() && OC.localStorage.hasItem(key)) {
OC.localStorage.removeItem(key); OC.localStorage.removeItem(key);
} }
@ -233,12 +255,13 @@ OC.Contacts = OC.Contacts || {};
* } * }
*/ */
Storage.prototype.getContacts = function(backend, addressBookId) { Storage.prototype.getContacts = function(backend, addressBookId) {
var headers = {}, var self = this,
headers = {},
data, data,
key = 'contacts::' + backend + '::' + addressBookId, key = 'contacts::' + backend + '::' + addressBookId,
defer = $.Deferred(); defer = $.Deferred();
if(OC.localStorage.hasItem(key)) { if(this.hasLocalStorage() && OC.localStorage.hasItem(key)) {
data = OC.localStorage.getItem(key); data = OC.localStorage.getItem(key);
headers['If-None-Match'] = data.Etag; headers['If-None-Match'] = data.Etag;
} }
@ -255,7 +278,9 @@ OC.Contacts = OC.Contacts || {};
console.log('Returning fetched address book'); console.log('Returning fetched address book');
if(response.data) { if(response.data) {
response.data.Etag = response.getResponseHeader('Etag'); response.data.Etag = response.getResponseHeader('Etag');
OC.localStorage.setItem(key, response.data); if (!self.hasLocalStorage()) {
OC.localStorage.setItem(key, response.data);
}
defer.resolve(response); defer.resolve(response);
} }
} else if(response.statusCode === 304) { } else if(response.statusCode === 304) {