From a8931c9021fa15190927b913a66fb34deedf9e20 Mon Sep 17 00:00:00 2001 From: Igor Minar Date: Wed, 22 Sep 2010 01:13:51 +0800 Subject: [PATCH] Rewrite session store service in object literal style and remove getAll method that is not used anywhere --- src/services.js | 35 +++++++++-------------------------- test/servicesSpec.js | 16 ++-------------- 2 files changed, 11 insertions(+), 40 deletions(-) diff --git a/src/services.js b/src/services.js index a0317f201a1e..f6dc931dd23d 100644 --- a/src/services.js +++ b/src/services.js @@ -429,35 +429,18 @@ angularService('$cookies', function($browser) { angularService('$sessionStore', function($store) { - function SessionStore() {} - - SessionStore.prototype.get = function(key) { - return fromJson($store[key]); - }; + return { + get: function(key) { + return fromJson($store[key]); + }, - SessionStore.prototype.getAll = function() { - var all = {}, - key; + put: function(key, value) { + $store[key] = toJson(value); + }, - for (key in $store) { - if (!$store.hasOwnProperty(key)) continue; - all[key] = fromJson($store[key]); + remove: function(key) { + delete $store[key]; } - - return all; - }; - - - SessionStore.prototype.put = function(key, value) { - $store[key] = toJson(value); - }; - - - SessionStore.prototype.remove = function(key) { - delete $store[key]; }; - - return new SessionStore(); - }, {inject: ['$cookies']}); diff --git a/test/servicesSpec.js b/test/servicesSpec.js index 6fa2c5f5ab54..28bde598f188 100644 --- a/test/servicesSpec.js +++ b/test/servicesSpec.js @@ -429,22 +429,11 @@ describe("service", function(){ it('should serialize objects to json', function() { scope.$sessionStore.put('objectCookie', {id: 123, name: 'blah'}); - scope.$eval(); + scope.$eval(); //force eval in test expect(scope.$browser.cookies()).toEqual({'objectCookie': '{"id":123,"name":"blah"}'}); }); - it('should return all persisted items as a has via getAll', function() { - expect(scope.$sessionStore.getAll()).toEqual({}); - - scope.$sessionStore.put('object1', {id:1,foo:'bar1'}); - scope.$sessionStore.put('object2', {id:2,foo:'bar2'}); - - expect(scope.$sessionStore.getAll()).toEqual({'object1':{id:1,foo:'bar1'}, - 'object2':{id:2,foo:'bar2'}}); - }); - - it('should deserialize json to object', function() { scope.$browser.cookies('objectCookie', '{"id":123,"name":"blah"}'); scope.$browser.poll(); @@ -454,8 +443,7 @@ describe("service", function(){ it('should delete objects from the store when remove is called', function() { scope.$sessionStore.put('gonner', { "I'll":"Be Back"}); - // TODO: Is this $eval necessary (why was it not here before?) - scope.$eval(); + scope.$eval(); //force eval in test expect(scope.$browser.cookies()).toEqual({'gonner': '{"I\'ll":"Be Back"}'}); });