forked from aranm/scalable-javascript-architecture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.Storage.js
More file actions
85 lines (82 loc) · 2.43 KB
/
Copy pathCore.Storage.js
File metadata and controls
85 lines (82 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
(function () {
var coreStorage = function () {
var fallbackStorage = { },
isLocalStorageSupported = (function() {
var isSupported;
try {
isSupported = 'localStorage' in window && window['localStorage'] !== null;
}
catch(e) {
isSupported = false;
}
return isSupported;
})(),
storage,
setItem = function(key, value) {
storage[key] = value;
},
getItem = function(key) {
var item = storage[key];
if (item === undefined) {
//localStorage returns null, not undefined if an item does not exist
//so do the same if using the fallback
item = null;
}
return item;
},
setObject = function(itemKey, value) {
storage[itemKey] = JSON.stringify(value);
},
getObject = function(key) {
var item = JSON.parse(getItem(key));
if (item === undefined) {
//localStorage returns null, not undefined if an item does not exist
//so do the same if using the fallback
item = null;
}
return item;
},
removeItem = function(key) {
if (storage === fallbackStorage) {
delete storage[key];
}
else {
storage.removeItem(key);
}
},
clear = function() {
if (storage === fallbackStorage) {
fallbackStorage = { };
storage = fallbackStorage;
}
else {
storage.clear();
}
};
if (isLocalStorageSupported === true) {
storage = localStorage;
}
else {
storage = fallbackStorage;
}
return {
storageHasNativeSupport: isLocalStorageSupported,
setItem: setItem,
getItem: getItem,
setObject: setObject,
getObject: getObject,
removeItem: removeItem,
clear: clear
};
};
if (typeof define === "function" && define.amd) {
define("Core.Storage", ["Core"], function (core) {
core.Storage = coreStorage();
return core.Storage;
});
}
else {
//we are going to attach this to the global Core object
Core.Storage = coreStorage();
}
})();