localStorage: remove dysfunctional fallback
This commit is contained in:
parent
51e5817cbd
commit
c422c2df4d
1 changed files with 29 additions and 39 deletions
|
@ -10,6 +10,10 @@ ls.factory("$store", ["$parse", function($parse){
|
||||||
var storage = (typeof window.localStorage === 'undefined') ? undefined : window.localStorage,
|
var storage = (typeof window.localStorage === 'undefined') ? undefined : window.localStorage,
|
||||||
supported = !(typeof storage == 'undefined' || typeof window.JSON == 'undefined');
|
supported = !(typeof storage == 'undefined' || typeof window.JSON == 'undefined');
|
||||||
|
|
||||||
|
if (!supported) {
|
||||||
|
console.log('Warning: localStorage is not supported');
|
||||||
|
}
|
||||||
|
|
||||||
var privateMethods = {
|
var privateMethods = {
|
||||||
/**
|
/**
|
||||||
* Pass any type of a string from the localStorage to be parsed so it returns a usable version (like an Object)
|
* Pass any type of a string from the localStorage to be parsed so it returns a usable version (like an Object)
|
||||||
|
@ -40,59 +44,45 @@ ls.factory("$store", ["$parse", function($parse){
|
||||||
};
|
};
|
||||||
var publicMethods = {
|
var publicMethods = {
|
||||||
/**
|
/**
|
||||||
* Set - let's you set a new localStorage key pair set
|
* Set - lets you set a new localStorage key pair set
|
||||||
* @param key - a string that will be used as the accessor for the pair
|
* @param key - a string that will be used as the accessor for the pair
|
||||||
* @param value - the value of the localStorage item
|
* @param value - the value of the localStorage item
|
||||||
* @returns {*} - will return whatever it is you've stored in the local storage
|
* @returns {*} - will return whatever it is you've stored in the local storage
|
||||||
*/
|
*/
|
||||||
set: function(key,value){
|
set: function(key,value){
|
||||||
if (!supported){
|
if (!supported){
|
||||||
try {
|
console.log('Local Storage not supported');
|
||||||
$.cookie(key, value);
|
|
||||||
return value;
|
|
||||||
} catch(e){
|
|
||||||
console.log('Local Storage not supported, make sure you have the $.cookie supported.');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
var saver = JSON.stringify(value);
|
var saver = JSON.stringify(value);
|
||||||
storage.setItem(key, saver);
|
storage.setItem(key, saver);
|
||||||
return privateMethods.parseValue(saver);
|
return privateMethods.parseValue(saver);
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* Get - let's you get the value of any pair you've stored
|
* Get - lets you get the value of any pair you've stored
|
||||||
* @param key - the string that you set as accessor for the pair
|
* @param key - the string that you set as accessor for the pair
|
||||||
* @returns {*} - Object,String,Float,Boolean depending on what you stored
|
* @returns {*} - Object,String,Float,Boolean depending on what you stored
|
||||||
*/
|
*/
|
||||||
get: function(key){
|
get: function(key){
|
||||||
if (!supported){
|
if (!supported){
|
||||||
try {
|
|
||||||
return privateMethods.parseValue($.cookie(key));
|
|
||||||
} catch(e){
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
var item = storage.getItem(key);
|
var item = storage.getItem(key);
|
||||||
return privateMethods.parseValue(item);
|
return privateMethods.parseValue(item);
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* Remove - let's you nuke a value from localStorage
|
* Remove - lets you nuke a value from localStorage
|
||||||
* @param key - the accessor value
|
* @param key - the accessor value
|
||||||
* @returns {boolean} - if everything went as planned
|
* @returns {boolean} - if everything went as planned
|
||||||
*/
|
*/
|
||||||
remove: function(key) {
|
remove: function(key) {
|
||||||
if (!supported){
|
if (!supported){
|
||||||
try {
|
|
||||||
$.cookie(key, null);
|
|
||||||
return true;
|
|
||||||
} catch(e){
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
storage.removeItem(key);
|
storage.removeItem(key);
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
* Bind - let's you directly bind a localStorage value to a $scope variable
|
* Bind - lets you directly bind a localStorage value to a $scope variable
|
||||||
* @param $scope - the current scope you want the variable available in
|
* @param $scope - the current scope you want the variable available in
|
||||||
* @param key - the name of the variable you are binding
|
* @param key - the name of the variable you are binding
|
||||||
* @param def - the default value (OPTIONAL)
|
* @param def - the default value (OPTIONAL)
|
||||||
|
|
Loading…
Reference in a new issue