aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMisko Hevery2010-09-14 22:51:01 +0200
committerMisko Hevery2010-09-14 22:51:01 +0200
commite3f760fbadedc977d9f5f461feafbaecab5a9046 (patch)
tree2d39d26b3b371649cfc6f0ababf055e50518f283 /src
parent07699b1a70f2a979ecd600c826ba89e79279925c (diff)
downloadangular.js-e3f760fbadedc977d9f5f461feafbaecab5a9046.tar.bz2
Adding cookie service
- Browser.cookies() - MockBrowser - $cookie service - $sessionStore
Diffstat (limited to 'src')
-rw-r--r--src/AngularPublic.js1
-rw-r--r--src/Browser.js50
-rw-r--r--src/services.js64
3 files changed, 115 insertions, 0 deletions
diff --git a/src/AngularPublic.js b/src/AngularPublic.js
index 7b093f88..e9f20b59 100644
--- a/src/AngularPublic.js
+++ b/src/AngularPublic.js
@@ -6,6 +6,7 @@ angularService('$browser', function browserFactory(){
jqLite(window.document),
jqLite(window.document.getElementsByTagName('head')[0]));
browserSingleton.startUrlWatcher();
+ browserSingleton.startCookieWatcher();
browserSingleton.bind();
}
return browserSingleton;
diff --git a/src/Browser.js b/src/Browser.js
index 46ac2bb0..0dacf3c4 100644
--- a/src/Browser.js
+++ b/src/Browser.js
@@ -22,8 +22,50 @@ function Browser(location, document, head) {
this.location = location;
this.document = document;
+ var rawDocument = document[0];
this.head = head;
this.idCounter = 0;
+
+ this.cookies = cookies;
+ this.watchCookies = function(fn){ cookieListeners.push(fn); };
+
+ // functions
+ var lastCookies = {};
+ var lastCookieString = '';
+ var cookieListeners = [];
+ /**
+ * cookies() -> hash of all cookies
+ * cookies(name, value) -> set name to value
+ * if value is undefined delete it
+ * cookies(name) -> should get value, but deletes (no one calls it right now that way)
+ */
+ function cookies(name, value){
+ if (name) {
+ if (value === _undefined) {
+ delete lastCookies[name];
+ rawDocument.cookie = escape(name) + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
+ } else {
+ rawDocument.cookie = escape(name) + '=' + escape(lastCookies[name] = ''+value);
+ }
+ } else {
+ if (rawDocument.cookie !== lastCookieString) {
+ lastCookieString = rawDocument.cookie;
+ var cookieArray = lastCookieString.split("; ");
+ lastCookies = {};
+
+ for (var i = 0; i < cookieArray.length; i++) {
+ var keyValue = cookieArray[i].split("=");
+ if (keyValue.length === 2) { //ignore nameless cookies
+ lastCookies[unescape(keyValue[0])] = unescape(keyValue[1]);
+ }
+ }
+ foreach(cookieListeners, function(fn){
+ fn(lastCookies);
+ });
+ }
+ return lastCookies;
+ }
+ }
}
Browser.prototype = {
@@ -132,6 +174,14 @@ Browser.prototype = {
})();
},
+ startCookieWatcher: function() {
+ var self = this;
+ (function poll() {
+ self.cookies();
+ self.setTimeout(poll, self.delay);
+ })();
+ },
+
setUrl: function(url) {
var existingURL = this.location.href;
if (!existingURL.match(/#/)) existingURL += '#';
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