var store = {
isArray:function(param){
return Array.isArray(param)
},
set:function(keyname,data){
localStorage.setItem(keyname,JSON.stringify(data));
},
get:function(keyname){
return JSON.parse(localStorage.getItem(keyname));
},
add:function(keyname,data){
var localData = this.get(keyname);
if(this.isArray(localData.dataObj)){
localData.dataObj.push(data)
} else{
localData.dataObj = data;
}
this.set(keyname,localData);
},
edit:function(keyname,idParam,data){
var localData = this.get(keyname);
if(this.isArray(localData.dataObj)){
localData.dataObj.forEach(function(v,k){
if(v.id === idParam){
localData.dataObj[k] = data
}
});
} else{
localData.dataObj = data;
}
this.set(keyname,localData);
},
delete:function(keyname,idParam){
var localData = this.get(keyname);
if(this.isArray(localData.dataObj)){
localData.dataObj.forEach(function(v,k){
if(v.id === idParam){
localData.dataObj.splice(k,1);
}
});
this.set(keyname,localData);
}
}
}
Gods, I was developing a project on mobile, and I encountered a lot of localstorage operations, so I encapsulated a method, which involves adding, deleting, modifying and querying. The dataObj here is a write-to-death array. Please help me to see if there is anything wrong with the encapsulation of this method. Or whether there is a suitable plug-in to encapsulate localStorage.