Created
February 8, 2012 08:43
-
-
Save bagwanpankaj/1766928 to your computer and use it in GitHub Desktop.
JS HTML5 storage helper
This file contains 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
app.storage = { | |
local: window.localStorage, // storage to be used | |
set: function( key, value ){ | |
this.local.setItem( key, JSON.stringify( value ) ); | |
$( document ).trigger( 'storage', ['set', key, value] ); | |
}, | |
get: function( key ){ | |
return JSON.parse( this.local.getItem( key ) ); | |
}, | |
key: function( index ){ | |
return this.local.key( index ); | |
}, | |
remove: function( key ){ | |
this.local.removeItem( key ); | |
$( document ).trigger( 'storage', ['remove', key] ); | |
}, | |
clear: function(){ | |
return this.local.clear(); | |
$( document ).trigger( 'storage', ['clear'] ); | |
}, | |
keys: function(){ | |
var res = []; | |
for( var i = 0; i < this.local.length; i++ ){ | |
res.push( this.local.key( i ) ); | |
}; | |
return res; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment