Rewrite session store service in object literal style and remove getAll method that is not used anywhere

This commit is contained in:
Igor Minar 2010-09-22 01:13:51 +08:00 committed by Misko Hevery
parent 7cb7fb91c6
commit a8931c9021
2 changed files with 11 additions and 40 deletions

View file

@ -429,35 +429,18 @@ angularService('$cookies', function($browser) {
angularService('$sessionStore', function($store) {
function SessionStore() {}
return {
get: function(key) {
return fromJson($store[key]);
},
SessionStore.prototype.get = function(key) {
return fromJson($store[key]);
};
put: function(key, value) {
$store[key] = toJson(value);
},
SessionStore.prototype.getAll = function() {
var all = {},
key;
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']});

View file

@ -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"}'});
});