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

Listen for change event for bound values #167

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 20 additions & 4 deletions dist/angular-local-storage.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* An Angular module that gives you access to the browsers local storage
* @version v0.1.5 - 2014-11-04
* @version v0.1.5 - 2014-11-27
* @link https://github.com/grevory/angular-local-storage
* @author grevory <[email protected]>
* @license MIT License, http://www.opensource.org/licenses/MIT
Expand Down Expand Up @@ -181,7 +181,6 @@ angularLocalStorage.provider('localStorageService', function() {
// Directly get a value from local storage
// Example use: localStorageService.get('library'); // returns 'angular'
var getFromLocalStorage = function (key) {

if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
if (!browserSupportsLocalStorage) {
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
Expand Down Expand Up @@ -408,12 +407,29 @@ angularLocalStorage.provider('localStorageService', function() {
} else if (isObject(value) && isObject(def)) {
value = extend(def, value);
}

$parse(key).assign(scope, value);

return scope.$watch(key, function(newVal) {
var onKeyUpdated = function (event) {
if (event.key == deriveQualifiedKey(key)) {
var updated = getFromLocalStorage(key);
if(scope[key] && typeof scope[key] === "object"){
angular.extend(scope[key], updated);
}
else {
scope[key] = updated;
}
scope.$apply();
}
};
$window.addEventListener("storage", onKeyUpdated, false);

var unregisterWatch = scope.$watch(key, function (newVal) {
addToLocalStorage(lsKey, newVal);
}, isObject(scope[key]));
return function () {
unregisterWatch();
$window.removeEventListener("storage", onKeyUpdated);
};
};

// Return localStorageService.length
Expand Down
4 changes: 2 additions & 2 deletions dist/angular-local-storage.min.js

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

22 changes: 19 additions & 3 deletions src/angular-local-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ angularLocalStorage.provider('localStorageService', function() {
// Directly get a value from local storage
// Example use: localStorageService.get('library'); // returns 'angular'
var getFromLocalStorage = function (key) {

if (!browserSupportsLocalStorage || self.storageType === 'cookie') {
if (!browserSupportsLocalStorage) {
$rootScope.$broadcast('LocalStorageModule.notification.warning','LOCAL_STORAGE_NOT_SUPPORTED');
Expand Down Expand Up @@ -381,12 +380,29 @@ angularLocalStorage.provider('localStorageService', function() {
} else if (isObject(value) && isObject(def)) {
value = extend(def, value);
}

$parse(key).assign(scope, value);

return scope.$watch(key, function(newVal) {
var onKeyUpdated = function (event) {
if (event.key == deriveQualifiedKey(key)) {
var updated = getFromLocalStorage(key);
if(scope[key] && typeof scope[key] === "object"){
angular.extend(scope[key], updated);
}
else {
scope[key] = updated;
}
scope.$apply();
}
};
$window.addEventListener("storage", onKeyUpdated, false);

var unregisterWatch = scope.$watch(key, function (newVal) {
addToLocalStorage(lsKey, newVal);
}, isObject(scope[key]));
return function () {
unregisterWatch();
$window.removeEventListener("storage", onKeyUpdated);
};
};

// Return localStorageService.length
Expand Down
55 changes: 53 additions & 2 deletions test/spec/localStorageSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,16 @@ describe('localStorageService', function() {
}

beforeEach(module('LocalStorageModule', function($provide) {

spyOn(window, 'addEventListener').andCallThrough();
spyOn(window, 'removeEventListener').andCallThrough();
$provide.value('$window', {
localStorage: localStorageMock()
localStorage: localStorageMock(),
addEventListener: function () {
window.addEventListener.apply(window, arguments);
},
removeEventListener : function (){
window.removeEventListener.apply(window, arguments);
}
});

}));
Expand Down Expand Up @@ -286,6 +293,7 @@ describe('localStorageService', function() {
$rootScope.$digest();

expect($rootScope.property).toEqual(localStorageService.get('property'));
expect(window.addEventListener).toHaveBeenCalled();
}));

it('should be able to unbind from scope variable', inject(function($rootScope, localStorageService) {
Expand All @@ -303,6 +311,7 @@ describe('localStorageService', function() {
$rootScope.$digest();

expect($rootScope.property).not.toEqual(localStorageService.get('property'));
expect(window.removeEventListener).toHaveBeenCalled();
}));

it('should be able to bind to properties of objects', inject(function($rootScope, localStorageService) {
Expand All @@ -318,6 +327,47 @@ describe('localStorageService', function() {
expect($rootScope.obj.property).toEqual(localStorageService.get('obj.property'));
}));

it('should update a bound value when local storage is updated', inject(function ($rootScope, localStorageService, $window){
localStorageService.bind($rootScope, 'test');
$rootScope.$digest();

// set the value in local storage mock to a value, then emit a changed event
var testValue = 'test';
$window.localStorage['ls.test'] = testValue;
var evt = document.createEvent('CustomEvent');
evt.key = 'ls.test';
evt.newValue = 'test value';
evt.initCustomEvent('storage', true, true, {
key: 'ls.test',
newValue: testValue
});
window.dispatchEvent(evt);
$rootScope.$digest();

expect($rootScope.test).toEqual(testValue);
}));

it("should keep unserializable properties of bound objects when local storage is updated", inject(function ($rootScope, localStorageService, $window){
localStorageService.bind($rootScope, 'test');
$rootScope.test = {obj: {a:1}, func: function (){}};
// set the value in local storage mock to a value, then emit a changed event
var testValue = JSON.stringify({obj:{a:2}});
$window.localStorage['ls.test'] = testValue;
var evt = document.createEvent('CustomEvent');
evt.key = 'ls.test';
evt.newValue = 'test value';
evt.initCustomEvent('storage', true, true, {
key: 'ls.test',
newValue: testValue
});
window.dispatchEvent(evt);
$rootScope.$digest();

expect($rootScope.test.obj).toEqual({a:2});
expect($rootScope.test.func).toBeDefined();
expect(typeof $rootScope.test.func).toBe('function');
}));

it('should be able to bind to scope using different key', inject(function($rootScope, localStorageService) {

localStorageService.set('lsProperty', 'oldValue');
Expand All @@ -329,6 +379,7 @@ describe('localStorageService', function() {
$rootScope.$digest();

expect($rootScope.property).toEqual(localStorageService.get('lsProperty'));
expect(window.addEventListener).toHaveBeenCalled();
}));

it('should $watch with deep comparison only for objects', inject(function($rootScope, localStorageService) {
Expand Down