Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serialize object and array values regardless of storage method #127

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions angular-local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ angularLocalStorage.provider('localStorageService', function() {
// Example use: localStorageService.add('library','angular');
var addToLocalStorage = function (key, value) {

var serializedValue = value;
// Serialize objects regardless of storage method
if (angular.isObject(value) || angular.isArray(value)) {
serializedValue = angular.toJson(value);
}

// If this browser does not support local storage use cookies
if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
if (!browserSupportsLocalStorage) {
Expand All @@ -125,25 +131,24 @@ angularLocalStorage.provider('localStorageService', function() {
if (notify.setItem) {
$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: 'cookie'});
}
return addToCookies(key, value);
return addToCookies(key, serializedValue);
}

// Let's convert undefined values to null to get the value consistent
if (typeof value === "undefined") {
value = null;
}

// Try local storage
try {
if (angular.isObject(value) || angular.isArray(value)) {
value = angular.toJson(value);
}
if (webStorage) {webStorage.setItem(deriveQualifiedKey(key), value)};
if (webStorage) {webStorage.setItem(deriveQualifiedKey(key), serializedValue)};
if (notify.setItem) {
$rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: self.storageType});
}
// Fallback to cookies
} catch (e) {
$rootScope.$broadcast('LocalStorageModule.notification.error', e.message);
return addToCookies(key, value);
return addToCookies(key, serializedValue);
}
return true;
};
Expand Down Expand Up @@ -329,7 +334,7 @@ angularLocalStorage.provider('localStorageService', function() {
thisCookie = thisCookie.substring(1,thisCookie.length);
}
if (thisCookie.indexOf(deriveQualifiedKey(key) + '=') === 0) {
return decodeURIComponent(thisCookie.substring(prefix.length + key.length + 1, thisCookie.length));
return angular.fromJSon(decodeURIComponent(thisCookie.substring(prefix.length + key.length + 1, thisCookie.length)));
}
}
return null;
Expand Down
2 changes: 1 addition & 1 deletion angular-local-storage.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.