-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStorageCtrl.js
More file actions
54 lines (54 loc) · 1.94 KB
/
StorageCtrl.js
File metadata and controls
54 lines (54 loc) · 1.94 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
//Storage Controller
const StorageCtrl = (function() {
//private data
//public methods
return {
storeItem: function(item) {
let items;
if(localStorage.getItem('items') === null) {
items = [];
//insert item into items
items.push(item);
//inserting items into local storage
localStorage.setItem('items', JSON.stringify(items));
} else {
//get items from local storage
items = JSON.parse(localStorage.getItem('items'));
//adding new item
items.push(item);
//inserting items into local storage
localStorage.setItem('items', JSON.stringify(items));
}
},
getItemsFromLS: function() {
let items;
if(localStorage.getItem('items') === null) {
items = [];
} else {
items = JSON.parse(localStorage.getItem('items'));
}
return items;
},
updateItemInLS: function(updatedItem) {
let items = JSON.parse(localStorage.getItem('items'));
items.forEach( (item, index) => {
if(item.id === updatedItem.id) {
items.splice(index, 1, updatedItem);
}
});
localStorage.setItem('items', JSON.stringify(items));
},
deleteItemFromLS: function(itemToBeDelID) {
let items = JSON.parse(localStorage.getItem('items'));
items.forEach( (item, index) => {
if(item.id === itemToBeDelID) {
items.splice(index, 1);
}
});
localStorage.setItem('items', JSON.stringify(items));
},
clearLS: function() {
localStorage.removeItem('items');
}
};
})();