2 * @license AngularJS v1.0.1
3 * (c) 2010-2012 Google, Inc. http://angularjs.org
6 (function(window, angular, undefined) {
15 angular.module('ngCookies', ['ng']).
18 * @name ngCookies.$cookies
22 * Provides read/write access to browser's cookies.
24 * Only a simple Object is exposed and by adding or removing properties to/from
25 * this object, new cookies are created/deleted at the end of current $eval.
29 factory('$cookies', ['$rootScope', '$browser', function ($rootScope, $browser) {
35 isUndefined = angular.isUndefined;
37 //creates a poller fn that copies all cookies from the $browser to service & inits the service
38 $browser.addPollFn(function() {
39 var currentCookies = $browser.cookies();
40 if (lastBrowserCookies != currentCookies) { //relies on browser.cookies() impl
41 lastBrowserCookies = currentCookies;
42 copy(currentCookies, lastCookies);
43 copy(currentCookies, cookies);
44 if (runEval) $rootScope.$apply();
50 //at the end of each eval, push cookies
51 //TODO: this should happen before the "delayed" watches fire, because if some cookies are not
52 // strings or browser refuses to store some cookies, we update the model in the push fn.
53 $rootScope.$watch(push);
59 * Pushes all the cookies from the service to the browser and verifies if all cookies were stored.
67 //delete any cookies deleted in $cookies
68 for (name in lastCookies) {
69 if (isUndefined(cookies[name])) {
70 $browser.cookies(name, undefined);
74 //update all cookies updated in $cookies
75 for(name in cookies) {
76 value = cookies[name];
77 if (!angular.isString(value)) {
78 if (angular.isDefined(lastCookies[name])) {
79 cookies[name] = lastCookies[name];
83 } else if (value !== lastCookies[name]) {
84 $browser.cookies(name, value);
89 //verify what was actually stored
92 browserCookies = $browser.cookies();
94 for (name in cookies) {
95 if (cookies[name] !== browserCookies[name]) {
96 //delete or reset all cookies that the browser dropped from $cookies
97 if (isUndefined(browserCookies[name])) {
100 cookies[name] = browserCookies[name];
112 * @name ngCookies.$cookieStore
116 * Provides a key-value (string-object) storage, that is backed by session cookies.
117 * Objects put or retrieved from this storage are automatically serialized or
118 * deserialized by angular's toJson/fromJson.
121 factory('$cookieStore', ['$cookies', function($cookies) {
126 * @name ngCookies.$cookieStore#get
127 * @methodOf ngCookies.$cookieStore
130 * Returns the value of given cookie key
132 * @param {string} key Id to use for lookup.
133 * @returns {Object} Deserialized cookie value.
136 return angular.fromJson($cookies[key]);
141 * @name ngCookies.$cookieStore#put
142 * @methodOf ngCookies.$cookieStore
145 * Sets a value for given cookie key
147 * @param {string} key Id for the `value`.
148 * @param {Object} value Value to be stored.
150 put: function(key, value) {
151 $cookies[key] = angular.toJson(value);
156 * @name ngCookies.$cookieStore#remove
157 * @methodOf ngCookies.$cookieStore
160 * Remove given cookie
162 * @param {string} key Id of the key-value pair to delete.
164 remove: function(key) {
165 delete $cookies[key];
171 })(window, window.angular);