Skip to content

Commit a8931c9

Browse files
IgorMinarmhevery
authored andcommitted
Rewrite session store service in object literal style and remove getAll method that is not used anywhere
1 parent 7cb7fb9 commit a8931c9

File tree

2 files changed

+11
-40
lines changed

2 files changed

+11
-40
lines changed

src/services.js

+9-26
Original file line numberDiff line numberDiff line change
@@ -429,35 +429,18 @@ angularService('$cookies', function($browser) {
429429

430430
angularService('$sessionStore', function($store) {
431431

432-
function SessionStore() {}
433-
434-
SessionStore.prototype.get = function(key) {
435-
return fromJson($store[key]);
436-
};
432+
return {
433+
get: function(key) {
434+
return fromJson($store[key]);
435+
},
437436

438-
SessionStore.prototype.getAll = function() {
439-
var all = {},
440-
key;
437+
put: function(key, value) {
438+
$store[key] = toJson(value);
439+
},
441440

442-
for (key in $store) {
443-
if (!$store.hasOwnProperty(key)) continue;
444-
all[key] = fromJson($store[key]);
441+
remove: function(key) {
442+
delete $store[key];
445443
}
446-
447-
return all;
448-
};
449-
450-
451-
SessionStore.prototype.put = function(key, value) {
452-
$store[key] = toJson(value);
453-
};
454-
455-
456-
SessionStore.prototype.remove = function(key) {
457-
delete $store[key];
458444
};
459445

460-
461-
return new SessionStore();
462-
463446
}, {inject: ['$cookies']});

test/servicesSpec.js

+2-14
Original file line numberDiff line numberDiff line change
@@ -429,22 +429,11 @@ describe("service", function(){
429429

430430
it('should serialize objects to json', function() {
431431
scope.$sessionStore.put('objectCookie', {id: 123, name: 'blah'});
432-
scope.$eval();
432+
scope.$eval(); //force eval in test
433433
expect(scope.$browser.cookies()).toEqual({'objectCookie': '{"id":123,"name":"blah"}'});
434434
});
435435

436436

437-
it('should return all persisted items as a has via getAll', function() {
438-
expect(scope.$sessionStore.getAll()).toEqual({});
439-
440-
scope.$sessionStore.put('object1', {id:1,foo:'bar1'});
441-
scope.$sessionStore.put('object2', {id:2,foo:'bar2'});
442-
443-
expect(scope.$sessionStore.getAll()).toEqual({'object1':{id:1,foo:'bar1'},
444-
'object2':{id:2,foo:'bar2'}});
445-
});
446-
447-
448437
it('should deserialize json to object', function() {
449438
scope.$browser.cookies('objectCookie', '{"id":123,"name":"blah"}');
450439
scope.$browser.poll();
@@ -454,8 +443,7 @@ describe("service", function(){
454443

455444
it('should delete objects from the store when remove is called', function() {
456445
scope.$sessionStore.put('gonner', { "I'll":"Be Back"});
457-
// TODO: Is this $eval necessary (why was it not here before?)
458-
scope.$eval();
446+
scope.$eval(); //force eval in test
459447
expect(scope.$browser.cookies()).toEqual({'gonner': '{"I\'ll":"Be Back"}'});
460448
});
461449

0 commit comments

Comments
 (0)