From 014c84239abaa09aebca73f72ff515cbe2dc3181 Mon Sep 17 00:00:00 2001 From: Kevin Dungs Date: Sun, 3 Aug 2014 23:04:46 +0200 Subject: [PATCH] Add ObjectStorage module. --- js/storage.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 js/storage.js diff --git a/js/storage.js b/js/storage.js new file mode 100644 index 0000000..d086da9 --- /dev/null +++ b/js/storage.js @@ -0,0 +1,25 @@ +'use strict'; + +/** Allows to save objects to HTML5 local storage. + * However, it can only save properties, not functions. + */ +var ObjectStorage = (function() { + try { + var _s = localStorage; + return { + save: function(key, item) { + _s.setItem(key, JSON.stringify(item)); + }, + load: function(key) { + return JSON.parse(_s.getItem(key)); + } + }; + } catch (e) { + alert('There is no local storage for you.' + + ' If you refresh the page, all progress will be lost'); + return { + save: function(key, item) {}, + load: function(key) { return null; } + }; + }; +});