Last active
August 29, 2015 14:18
-
-
Save skelz0r/7110873b8173dc354303 to your computer and use it in GitHub Desktop.
Devoxx 2015 : Ionic lab - Storage service
This file contains hidden or 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.module('app') | |
.factory('Storage', function($window){ | |
'use strict'; | |
var localStorageFallback = {}; | |
var service = { | |
get: get, | |
set: set | |
}; | |
function get(key){ | |
if($window.localStorage){ | |
try { | |
return JSON.parse($window.localStorage.getItem(key)); | |
} catch(e) { | |
return null; | |
} | |
} else { | |
return angular.copy(localStorageFallback[key]); | |
} | |
} | |
function set(key, value){ | |
if($window.localStorage){ | |
$window.localStorage.setItem(key, JSON.stringify(value)); | |
} else { | |
localStorageFallback[key] = angular.copy(value); | |
} | |
} | |
return service; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment