Last active
October 20, 2015 08:32
-
-
Save renesansz/eb915298018a6da53895 to your computer and use it in GitHub Desktop.
Local Storage Service Provider
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
/** | |
* 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