Skip to content

Instantly share code, notes, and snippets.

@mobz
Created June 24, 2013 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mobz/5847155 to your computer and use it in GitHub Desktop.
Save mobz/5847155 to your computer and use it in GitHub Desktop.
localStorage api
this.api = (function() {
var key = localStorage.getItem("_key") || 0;
return {
put: function( o, cb ) {
localStorage.setItem("_key", key++ );
localStorage.setItem( "squares:" + key, JSON.stringify( o ) );
setTimeout( function() {
cb(key);
}, 250 );
},
get: function( cb ) {
var l = localStorage.length;
var results = [];
for( var i = 0; i < l; i++) {
var k = localStorage.key(i);
if( /squares/.test(k) ) {
results.push( { id: k.match(/\d+/), square: JSON.parse( localStorage.getItem( k ) ) } );
}
}
setTimeout( function() {
cb( results );
}, 250 );
},
delete: function( id ) {
localStorage.removeItem( "squares:" + id );
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment