/
persistence.js
executable file
·49 lines (44 loc) · 1.31 KB
/
persistence.js
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
Persistence = {
load: function (key) {
return new ValueWrapper(key);
},
/* Creating helper methods for each persistence entry (load() shoudn't be used directly) */
init: function() {
var existingKeys = ['options', 'selected_lists', 'timeline_order', 'oauth_token_data',
'oauth_token_service', 'version', 'popup_size'];
var _this = this;
for(var i = 0, len = existingKeys.length; i < len; ++i) {
var currentKey = existingKeys[i];
var methodName = currentKey.replace(/_(\w)/g, function(m1, m2) { return m2.toUpperCase(); });
/* Ohh, so sad I can't just use the 'let' keyword */
this[methodName] = (function(key) {
return function() {
return _this.load(key);
}
})(currentKey);
}
},
cleanupOldData: function() {
localStorage.removeItem('password');
localStorage.removeItem('logged');
localStorage.removeItem('username');
localStorage.removeItem('remember');
localStorage.removeItem('currentTheme');
}
};
Persistence.init();
function ValueWrapper(key) {
this.key = key;
}
ValueWrapper.prototype = {
save: function(value) {
localStorage[this.key] = value;
return value;
},
val: function() {
return localStorage[this.key];
},
remove: function() {
return localStorage.removeItem(this.key);
}
};