JavaScript: Light weight wrapper class for window.localStorage

Recently I wrote a small wrapper class around the window.localStorage with useful functionality apart from normal get/set value.

For example
  • Accessors for JSON/Boolean/Number,
  • Event registering for type create/update/remove/clear events

API functions

LS.set(key, value)
LS.setJson(key, jsonObject)

LS.get(key): String
LS.getJson(key): JsonObject
LS.getNumber(key): Int
LS.getBoolean(key): Boolean

//Event API
LS.on(event, listenerFunction, scope): function //this return function can be used for unregistering from event

//different type and listeners callback param
events type: 'create'   listenerFunction callback param: key, value
events type: 'update'   listenerFunction callback param: key, newValue, oldValue
events type: 'remove'   listenerFunction callback param: key
events type: 'clear'    listenerFunction callback param:

//remove all the registered event for given event type
Event registering/unregistering example
//registering listener
var updateListenerFn = LS.on('update', function(key, newValue, oldValue){

//unregistering listener
LS.un('update', updateListenerFn);
Hope it will be useful for others. 


Popular posts from this blog

ERROR: Ignored call to 'alert()'. The document is sandboxed, and the 'allow-modals' keyword is not set.

Application Design Notes

How to store user password at server!!!