From a425b43caab62f01d394dd1afcf4148e8d5cb332 Mon Sep 17 00:00:00 2001 From: Ariel Mashraki Date: Sun, 12 Oct 2014 01:48:46 +0300 Subject: [PATCH] feat(localStorageService): improve .bind func, issue #144 --- src/angular-local-storage.js | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/angular-local-storage.js b/src/angular-local-storage.js index 6b00fec..c7a0b55 100644 --- a/src/angular-local-storage.js +++ b/src/angular-local-storage.js @@ -367,11 +367,8 @@ angularLocalStorage.provider('localStorageService', function() { // Add a listener on scope variable to save its changes to local storage // Return a function which when called cancels binding - var bindToScope = function(scope, scopeKey, def, lsKey) { - if (!lsKey) { - lsKey = scopeKey; - } - + var bindToScope = function(scope, key, def, lsKey) { + lsKey = lsKey || key; var value = getFromLocalStorage(lsKey); if (value === null && isDefined(def)) { @@ -380,11 +377,11 @@ angularLocalStorage.provider('localStorageService', function() { value = extend(def, value); } - $parse(scopeKey).assign(scope, value); + $parse(key).assign(scope, value); - return scope.$watchCollection(scopeKey, function(newVal) { + return scope.$watch(key, function(newVal) { addToLocalStorage(lsKey, newVal); - }); + }, isObject(scope[key])); }; // Return localStorageService.length