Created
November 11, 2013 16:26
-
-
Save iddar/7415990 to your computer and use it in GitHub Desktop.
Implementacion de localStorage para el almacenamiento de objetos JSON de manera facil.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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