Skip to content

Instantly share code, notes, and snippets.

@renesansz
Last active October 20, 2015 08:32
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 renesansz/eb915298018a6da53895 to your computer and use it in GitHub Desktop.
Save renesansz/eb915298018a6da53895 to your computer and use it in GitHub Desktop.
Local Storage Service Provider
/**
* Angular App Factory: LocalStorage
*/
(function() {
'use strict';
angular.module('app.factories')
.factory('LocalStorage', LocalStorage);
function LocalStorage($window) {
var factory = {};
/////////////////////
// Service Methods //
/////////////////////
factory.RemoveItem = RemoveItem;
factory.GetObject = GetObject;
factory.Clear = Clear;
factory.Set = Set;
factory.Get = Get;
return factory;
//////////////////////////
// Function Definitions //
//////////////////////////
/**
* Store a value in to window.localStorage
*
* @param {String} key - Unique identifier for the storage
* @param {Any} val - The value to be stored
*/
function Set(key, val) {
if (typeof val === 'object')
$window.localStorage.setItem(key, JSON.stringify(val));
else
$window.localStorage.setItem(key, val);
}
/**
* Gets the data from window.localStorage
*
* @param {String} key - Unique identifer for the storage
*
* @return {Any except Object}
*/
function Get(key) {
return $window.localStorage[key];
}
/**
* Gets the data object from window.localStorage
*
* @param {String} key - Unique identifer for the storage
*
* @return {Object}
*/
function GetObject(key) {
return ($window.localStorage[key] === 'undefined') ? null : JSON.parse($window.localStorage[key] || null);
}
/**
* Removes the data in window.localStorage
*
* @param {String} key - Unique identifer for the storage
*/
function RemoveItem(key) {
$window.localStorage.removeItem(key);
}
/**
* Clears the data in window.localStorage
*/
function Clear() {
$window.localStorage.clear();
}
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment