From d693b784412a0b80501689739f54db9cbff9089c Mon Sep 17 00:00:00 2001 From: Kevin Lubick Date: Thu, 9 Oct 2014 11:43:48 -0400 Subject: [PATCH 1/4] Added in deep compare option for bind It will be slower, but allows for binding an array objects. http://stackoverflow.com/a/14713978/1447621 --- src/angular-local-storage.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/angular-local-storage.js b/src/angular-local-storage.js index 9b2e58e..23f5f20 100644 --- a/src/angular-local-storage.js +++ b/src/angular-local-storage.js @@ -365,7 +365,7 @@ angularLocalStorage.provider('localStorageService', function() { return storageType; }; - var bindToScope = function(scope, scopeKey, def, lsKey) { + var bindToScope = function(scope, scopeKey, def, lsKey, useDeepCompare) { if (!lsKey) { lsKey = scopeKey; } @@ -380,9 +380,15 @@ angularLocalStorage.provider('localStorageService', function() { $parse(scopeKey).assign(scope, value); - scope.$watchCollection(scopeKey, function(newVal) { - addToLocalStorage(lsKey, newVal); - }); + if (!useDeepCompare) { + scope.$watchCollection(scopeKey, function(newVal) { + addToLocalStorage(lsKey, newVal); + }); + } else { + scope.$watch(scopeKey, function(newVal) { + addToLocalStorage(lsKey, newVal); + }, true); + } }; // Return localStorageService.length From e8ecccff66456f0b0e4f6d14f42899217ec63dc3 Mon Sep 17 00:00:00 2001 From: Kevin Lubick Date: Thu, 9 Oct 2014 11:44:04 -0400 Subject: [PATCH 2/4] Don't need
. Just end a line with two spaces --- README.md | 57 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 5102e44..4287bcc 100644 --- a/README.md +++ b/README.md @@ -32,9 +32,9 @@ An Angular module that gives you access to the browsers local storage, **v0.1.1* - [clearAll](#cookieclearall) ##Get Started -**(1)** You can install angular-local-storage using 2 different ways:
+**(1)** You can install angular-local-storage using 2 different ways: **Git:** -clone & build [this](https://github.com/grevory/angular-local-storage.git) repository
+clone & build [this](https://github.com/grevory/angular-local-storage.git) repository **Bower:** ```bash $ bower install angular-local-storage @@ -70,7 +70,7 @@ When you're done, your setup should look similar to the following: ``` ##Configuration ###setPrefix -You could set a prefix to avoid overwriting any local storage variables from the rest of your app
+You could set a prefix to avoid overwriting any local storage variables from the rest of your app **Default prefix:** `ls.` ```js myApp.config(function (localStorageServiceProvider) { @@ -79,7 +79,7 @@ myApp.config(function (localStorageServiceProvider) { }); ``` ###setStorageType -You could change web storage type to localStorage or sessionStorage
+You could change web storage type to localStorage or sessionStorage **Default storage:** `localStorage` ```js myApp.config(function (localStorageServiceProvider) { @@ -88,8 +88,8 @@ myApp.config(function (localStorageServiceProvider) { }); ``` ###setStorageCookie -Set cookie options (usually in case of fallback)
-**expiry:** number of days before cookies expire (0 = does not expire). **default:** `30`
+Set cookie options (usually in case of fallback) +**expiry:** number of days before cookies expire (0 = does not expire). **default:** `30` **path:** the web path the cookie represents. **default:** `'/'` ```js myApp.config(function (localStorageServiceProvider) { @@ -98,7 +98,7 @@ myApp.config(function (localStorageServiceProvider) { }); ``` ###setStorageCookieDomain -Set for cookie domain
+Set for cookie domain **No default value** ```js myApp.config(function (localStorageServiceProvider) { @@ -107,8 +107,8 @@ myApp.config(function (localStorageServiceProvider) { }); ``` ###setNotify -Send signals for each of the following actions:
-**setItem** , default: `true`
+Send signals for each of the following actions: +**setItem** , default: `true` **removeItem** , default: `false` ```js myApp.config(function (localStorageServiceProvider) { @@ -149,8 +149,8 @@ myApp.controller('MainCtrl', function($scope, localStorageService) { }); ``` ###set -Directly adds a value to local storage.
-If local storage is not supported, use cookies instead.
+Directly adds a value to local storage. +If local storage is not supported, use cookies instead. **Returns:** `Boolean` ```js myApp.controller('MainCtrl', function($scope, localStorageService) { @@ -162,8 +162,8 @@ myApp.controller('MainCtrl', function($scope, localStorageService) { }); ``` ###get -Directly get a value from local storage.
-If local storage is not supported, use cookies instead.
+Directly get a value from local storage. +If local storage is not supported, use cookies instead. **Returns:** `value from local storage` ```js myApp.controller('MainCtrl', function($scope, localStorageService) { @@ -175,7 +175,7 @@ myApp.controller('MainCtrl', function($scope, localStorageService) { }); ``` ###keys -Return array of keys for local storage, ignore keys that not owned.
+Return array of keys for local storage, ignore keys that not owned. **Returns:** `value from local storage` ```js myApp.controller('MainCtrl', function($scope, localStorageService) { @@ -185,8 +185,8 @@ myApp.controller('MainCtrl', function($scope, localStorageService) { }); ``` ###remove -Remove an item from local storage by key.
-If local storage is not supported, use cookies instead.
+Remove an item from local storage by key. +If local storage is not supported, use cookies instead. **Returns:** `Boolean` ```js myApp.controller('MainCtrl', function($scope, localStorageService) { @@ -198,9 +198,9 @@ myApp.controller('MainCtrl', function($scope, localStorageService) { }); ``` ###clearAll -Remove all data for this app from local storage.
-If local storage is not supported, use cookies instead.
-**Note:** Optionally takes a regular expression string and removes matching.
+Remove all data for this app from local storage. +If local storage is not supported, use cookies instead. +**Note:** Optionally takes a regular expression string and removes matching. **Returns:** `Boolean` ```js myApp.controller('MainCtrl', function($scope, localStorageService) { @@ -215,12 +215,15 @@ myApp.controller('MainCtrl', function($scope, localStorageService) { }); ``` ###bind -Bind $scope key to localStorageService. +Binds $scope key to localStorageService. + +bind(scope,) + ```js myApp.controller('MainCtrl', function($scope, localStorageService) { //... localStorageService.set('property', 'oldValue'); - localStorageService.bind($rootScope, 'property'); + localStorageService.bind($rootScope, 'property'); //Test Changes $rootScope.property = 'newValue'; @@ -253,8 +256,8 @@ myApp.controller('MainCtrl', function($scope, localStorageService) { ##Cookie Deal with browser's cookies directly. ###cookie.set -Directly adds a value to cookies.
-**Note:** Typically used as a fallback if local storage is not supported.
+Directly adds a value to cookies. +**Note:** Typically used as a fallback if local storage is not supported. **Returns:** `Boolean` ```js myApp.controller('MainCtrl', function($scope, localStorageService) { @@ -266,7 +269,7 @@ myApp.controller('MainCtrl', function($scope, localStorageService) { }); ``` ###cookie.get -Directly get a value from a cookie.
+Directly get a value from a cookie. **Returns:** `value from local storage` ```js myApp.controller('MainCtrl', function($scope, localStorageService) { @@ -278,7 +281,7 @@ myApp.controller('MainCtrl', function($scope, localStorageService) { }); ``` ###cookie.remove -Remove directly value from a cookie.
+Remove directly value from a cookie. **Returns:** `Boolean` ```js myApp.controller('MainCtrl', function($scope, localStorageService) { @@ -290,7 +293,7 @@ myApp.controller('MainCtrl', function($scope, localStorageService) { }); ``` ###clearAll -Remove all data for this app from cookie.
+Remove all data for this app from cookie. ```js myApp.controller('MainCtrl', function($scope, localStorageService) { //... @@ -316,7 +319,7 @@ Run the tests: ```bash $ grunt test ``` -**Deploy:**
+**Deploy:** Run the build task, update version before(bower,package) ```bash $ grunt dist From f7f4384a41678cfacfae4e50492d1965b875ef0e Mon Sep 17 00:00:00 2001 From: Kevin Lubick Date: Thu, 9 Oct 2014 12:03:29 -0400 Subject: [PATCH 3/4] Made bind documentation more robust Documented the newly added feature and the other arguments as well. --- README.md | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 4287bcc..81e0144 100644 --- a/README.md +++ b/README.md @@ -128,7 +128,7 @@ myApp.config(function (localStorageServiceProvider) { ``` ##API Documentation ##isSupported -Checks if the browser support the current storage type(e.g: `localStorage`, `sessionStorage`). +Checks if the browser support the current storage type(e.g: `localStorage`, `sessionStorage`). **Returns:** `Boolean` ```js myApp.controller('MainCtrl', function($scope, localStorageService) { @@ -215,10 +215,14 @@ myApp.controller('MainCtrl', function($scope, localStorageService) { }); ``` ###bind -Binds $scope key to localStorageService. - -bind(scope,) +Binds $scope or $rootScope key to an element stored in localStorageService. +**scope** : The location of the bound element, either $scope or $rootScope. +**scopeKey** : The name of the scoped element. E.g. for $scope.foo, this should be "foo". This doesn't need to be defined already. +**defaultValue** : [optional] If the value doesn't exist in localstorage, what should it default to? Defaults to null. +**localStorageKey** : [optional] The local storage key that maps to the bound value. By default, this is the same as scopeKey. +**useDeepCompare** : [optional] Should a more sensitve listener be used? Use true if you are binding an array of objects, or a similarly nested problem. Defaults to false. +#####A simple example binding $rootScope.property to a value. ```js myApp.controller('MainCtrl', function($scope, localStorageService) { //... @@ -231,8 +235,26 @@ myApp.controller('MainCtrl', function($scope, localStorageService) { //... }); ``` +#####A more complex example using the deeply nested comparison +```js +myApp.controller('MainCtrl', function($scope, localStorageService) { + //... + localStorageService.bind($scope, 'people', + [{name:"John", accountBalance:200},{name:"Mary", accountBalance:7000}], + 'members_account_balances', true); + //$scope.people is now bound to localstorage.members_account_balances + + //Test Changes + $scope.people[1].accountBalance = 6500; //this won't be detected if useDeepCompare == false + + console.log(localStorageService.get('members_account_balances')) // Mary's balance will now be 6500 + //... +}); +``` + + ###deriveKey -Return the derive key +Return the derive key **Returns** `String` ```js myApp.controller('MainCtrl', function($scope, localStorageService) { @@ -244,7 +266,7 @@ myApp.controller('MainCtrl', function($scope, localStorageService) { }); ``` ###length -Return localStorageService.length, ignore keys that not owned. +Return localStorageService.length, ignore keys that not owned. **Returns** `Number` ```js myApp.controller('MainCtrl', function($scope, localStorageService) { From f2414c134ac09861a858bccb833a50ffa7d5ae49 Mon Sep 17 00:00:00 2001 From: Kevin Lubick Date: Fri, 10 Oct 2014 08:41:15 -0400 Subject: [PATCH 4/4] Added test --- README.md | 2 +- test/spec/localStorageSpec.js | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 81e0144..32323c9 100644 --- a/README.md +++ b/README.md @@ -247,7 +247,7 @@ myApp.controller('MainCtrl', function($scope, localStorageService) { //Test Changes $scope.people[1].accountBalance = 6500; //this won't be detected if useDeepCompare == false - console.log(localStorageService.get('members_account_balances')) // Mary's balance will now be 6500 + console.log(localStorageService.get('members_account_balances')); // Mary's balance will now be 6500 //... }); ``` diff --git a/test/spec/localStorageSpec.js b/test/spec/localStorageSpec.js index adfe4d4..f8f1149 100644 --- a/test/spec/localStorageSpec.js +++ b/test/spec/localStorageSpec.js @@ -302,8 +302,29 @@ describe('localStorageService', function() { expect($rootScope.property).toEqual(localStorageService.get('lsProperty')); })); + + it('should be able to bind a complicated object/array to scope', inject(function($rootScope, localStorageService) { + + + + localStorageService.set('members_account_balances', [{name:'John', accountBalance:200},{name:'Mary', accountBalance:7000}]); + + localStorageService.bind($rootScope, 'people', undefined, + 'members_account_balances', true); + + expect($rootScope.people).toEqual(localStorageService.get('members_account_balances')); + + $rootScope.people[1].accountBalance = 6500; //this may not be detected if useDeepCompare == false + + $rootScope.$digest(); + + expect($rootScope.people).toEqual(localStorageService.get('members_account_balances')); + + expect($rootScope.people[1].accountBalance).toEqual( + localStorageService.get('members_account_balances')[1].accountBalance); + })); - it('should be able to return it\'s owned keys amount', inject( + it('should be able to return its owned keys amount', inject( function(localStorageService, $window) { for(var i = 0; i < 10; i++) {