FIX: ensure we can write to the localStorage

This commit is contained in:
Régis Hanol 2015-10-13 10:21:49 +02:00
parent a56e3ccea0
commit cafff9bf01
2 changed files with 7 additions and 7 deletions
app/assets/javascripts/discourse/lib

View file

@ -87,7 +87,7 @@ var initializeUngroupedIcons = function(){
try {
if (localStorage && !localStorage.emojiUsage) { localStorage.emojiUsage = "{}"; }
} catch(e){
} catch (e) {
/* localStorage can be disabled, or cookies disabled, do not crash script here
* TODO introduce a global wrapper for dealing with local storage
* */

View file

@ -5,13 +5,15 @@ try {
safeLocalStorage = localStorage;
if (localStorage["disableLocalStorage"] === "true") {
safeLocalStorage = null;
} else {
// makes sure we can write to the local storage
safeLocalStorage["safeLocalStorage"] = true;
}
} catch(e){
} catch (e) {
// cookies disabled, we don't care
safeLocalStorage = null;
}
const KeyValueStore = function(ctx) {
this.context = ctx;
};
@ -56,9 +58,8 @@ KeyValueStore.prototype = {
getObject(key) {
if (!safeLocalStorage) { return null; }
try {
return JSON.parse(safeLocalStorage[this.context + key]);
} catch(e) {}
try { return JSON.parse(safeLocalStorage[this.context + key]); }
catch (e) {}
}
};
@ -69,5 +70,4 @@ KeyValueStore.prototype.setItem = function(key, value) {
this.set({ key, value });
};
export default KeyValueStore;