diff options
| author | Misko Hevery | 2010-09-14 22:51:01 +0200 |
|---|---|---|
| committer | Misko Hevery | 2010-09-14 22:51:01 +0200 |
| commit | e3f760fbadedc977d9f5f461feafbaecab5a9046 (patch) | |
| tree | 2d39d26b3b371649cfc6f0ababf055e50518f283 /src/services.js | |
| parent | 07699b1a70f2a979ecd600c826ba89e79279925c (diff) | |
| download | angular.js-e3f760fbadedc977d9f5f461feafbaecab5a9046.tar.bz2 | |
Adding cookie service
- Browser.cookies()
- MockBrowser
- $cookie service
- $sessionStore
Diffstat (limited to 'src/services.js')
| -rw-r--r-- | src/services.js | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/src/services.js b/src/services.js index 19375f39..41f179e9 100644 --- a/src/services.js +++ b/src/services.js @@ -392,3 +392,67 @@ angularService('$resource', function($xhr){ var resource = new ResourceFactory($xhr); return bind(resource, resource.route); }, {inject: ['$xhr.cache']}); + + +angularService('$cookies', function($browser) { + var cookies = {}, rootScope = this; + $browser.watchCookies(function(newCookies){ + copy(newCookies, cookies); + rootScope.$eval(); + }); + this.$onEval(PRIORITY_FIRST, update); + this.$onEval(PRIORITY_LAST, update); + return cookies; + + function update(){ + var name, browserCookies = $browser.cookies(); + for(name in cookies) { + if (browserCookies[name] !== cookies[name]) { + $browser.cookies(name, browserCookies[name] = cookies[name]); + } + } + for(name in browserCookies) { + if (browserCookies[name] !== cookies[name]) { + $browser.cookies(name, _undefined); + //TODO: write test for this delete + //delete cookies[name]; + } + } + }; +}, {inject: ['$browser']}); + + +angularService('$sessionStore', function($store) { + + function SessionStore() {} + + SessionStore.prototype.get = function(key) { + return fromJson($store[key]); + }; + + SessionStore.prototype.getAll = function() { + var all = {}, + key; + + for (key in $store) { + if (!$store.hasOwnProperty(key)) continue; + all[key] = fromJson($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']});
\ No newline at end of file |
