Created
November 28, 2016 09:46
-
-
Save simonwoo/ae709ceaa7a16474ed62d8fad30a6c88 to your computer and use it in GitHub Desktop.
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('localstorage') | |
.factory('storageHelper', function ($cacheFactory, $window) { | |
'use strict'; | |
var memoryStorage = $cacheFactory('XXX'); | |
var localStorage = $window.localStorage; | |
return { | |
storage: getStorage() | |
}; | |
function getStorage () { | |
if (isLocalStorageSupported()) { | |
// return localstorage directly, | |
// because chrome does not support method forwarding on localstorage | |
return localStorage; | |
} else { | |
// use memory storage, | |
// because safari in private mode does not allow the use of localstorage | |
return { | |
getItem: memoryStorage.get, | |
setItem: memoryStorage.put, | |
removeItem: memoryStorage.remove | |
}; | |
} | |
} | |
function isLocalStorageSupported () { | |
try { | |
localStorage.setItem('test', 'testValue'); | |
localStorage.removeItem('test'); | |
return true; | |
} catch (error) { | |
return false; | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment