From bd5bc4a4aeb2c353c7279cd6dabffafa3081248b Mon Sep 17 00:00:00 2001 From: Nachiket Mehta Date: Fri, 21 Jun 2013 16:30:15 -0500 Subject: [PATCH] Detecting if the value being stored is an object. using angular's fronJson and toJson methods to parse it. This saves the user from having to call the stringyfyJson() and parseJson() before getting and setting to the local storage. Those functions can also be now ported over to use angular.toJson and angular.fromJson(). That change is not included in this commit. --- localStorageModule.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/localStorageModule.js b/localStorageModule.js index 399040b..01584ea 100644 --- a/localStorageModule.js +++ b/localStorageModule.js @@ -52,6 +52,9 @@ angularLocalStorage.service('localStorageService', [ if (typeof value == "undefined") value = null; try { + if (angular.isObject(value)) { + value = angular.toJson(value); + } localStorage.setItem(prefix+key, value); if (notify.setItem) { $rootScope.$broadcast('LocalStorageModule.notification.setitem', {key: key, newvalue: value, storageType: 'localStorage'}); @@ -73,6 +76,9 @@ angularLocalStorage.service('localStorageService', [ var item = localStorage.getItem(prefix+key); if (!item) return null; + if (item.charAt(0) === "{") { + return angular.fromJson(item); + } return item; };