-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage.js
46 lines (42 loc) · 1.6 KB
/
storage.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
class Storage{
static addListToStorage(newList){
// oluşturduğumuz array i bir değişkene atadık
let list = this.getListFromStorage();
// array e ekleme işlemi yapıyoruz
list.push(newList);
// üzerinde değişiklik yaptığımız listeyi tekrar storage e ekleyip
// array i string e çeviriyoruz
localStorage.setItem("list",JSON.stringify(list));
};
// listeyi getirme işlemi
static getListFromStorage(){
let list;
// liste localStorage da var mı yok mu kontrolünü sağla
// eğer liste yok ise boş bir array oluşturuyoruz
if(localStorage.getItem("list") === null){
list = [];
}
else {
// var ise bu listeyi yani string değeri alıp array e çeviriyoruz
list = JSON.parse(localStorage.getItem("list"));
}
return list;
};
// storage den silme işlemi
static deleteListFromStorage(listTitle){
let lists = this.getListFromStorage();
// burada tekli silme işlemi olduğu için index kullanıyoruz
lists.forEach(function(list,index){
if(list.need === listTitle){
// splice array den silme işlemini gerçekleştirir
lists.splice(index,1); // indexten itibaren, 1 tane silinecek
}
});
// silme işlemi gerçekleştikten sonra array i localStorage e yazıoyruz
localStorage.setItem("list",JSON.stringify(lists));
};
// bütün listeyi silme işlemi
static deleteAllListFromStorage(){
localStorage.removeItem("list");
}
};