-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp-session.html
55 lines (47 loc) · 1.14 KB
/
app-session.html
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
<link rel="import" href="../polymer/polymer.html">
<script>
Polymer({
is: 'app-session',
properties: {
prefix: {
type: String,
value: 'session_',
readOnly: true
},
storage: {
type: Object,
value: function () {
return window.localStorage;
}
}
},
has: function (key) {
var prefixed_key = this.prefix + key;
var value = this.storage.getItem(prefixed_key);
return value !== undefined;
},
get: function (key) {
var prefixed_key = this.prefix + key;
var value = this.storage.getItem(prefixed_key);
var parsed;
try {
parsed = JSON.parse(value);
} catch (e) {
parsed = null;
}
return parsed;
},
set: function (key, value) {
var prefixed_key = this.prefix + key;
var stringfied = JSON.stringify(value);
this.storage.setItem(prefixed_key, stringfied);
},
unset: function (key) {
var prefixed_key = this.prefix + key;
this.storage.removeItem(prefixed_key);
},
clear: function () {
this.storage.clear();
}
});
</script>