Skip to content

Instantly share code, notes, and snippets.

@iddar
Created November 11, 2013 16:26
Show Gist options
  • Select an option

  • Save iddar/7415990 to your computer and use it in GitHub Desktop.

Select an option

Save iddar/7415990 to your computer and use it in GitHub Desktop.
Implementacion de localStorage para el almacenamiento de objetos JSON de manera facil.
function db(_name){
this.name = _name;
this.length = function(){
return this.data.length;
}
this.set = function(_data){
this.data.push(_data);
var data_string = JSON.stringify(this.data);
localStorage.setItem(this.name, data_string);
}
this.get = function(){
var data_string = localStorage.getItem(this.name);
return JSON.parse( data_string );
}
this.clear = function() {
localStorage.setItem(this.name, null);
}
this.data = this.get() || [];
}
var clases = new db("clases");
var algo = new db("algo")
if(localStorage){
clases.set( { hr:10, nombre:"mate2" } );
algo.set( { hr:12, nombre:"espa" } );
for(var i in clases.data ){
console.log(clases.data[i]);
}
//clases.clear();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment