Skip to content

Commit

Permalink
Rewrite session store service in object literal style and remove getA…
Browse files Browse the repository at this point in the history
…ll method that is not used anywhere
  • Loading branch information
IgorMinar authored and mhevery committed Sep 23, 2010
1 parent 7cb7fb9 commit a8931c9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 40 deletions.
35 changes: 9 additions & 26 deletions src/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -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']});
16 changes: 2 additions & 14 deletions test/servicesSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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"}'});
});

Expand Down

0 comments on commit a8931c9

Please sign in to comment.