2 * @license AngularJS v1.2.25
\r
3 * (c) 2010-2014 Google, Inc. http://angularjs.org
\r
6 (function(window, angular, undefined) {'use strict';
\r
15 * The `ngCookies` module provides a convenient wrapper for reading and writing browser cookies.
\r
18 * <div doc-module-components="ngCookies"></div>
\r
20 * See {@link ngCookies.$cookies `$cookies`} and
\r
21 * {@link ngCookies.$cookieStore `$cookieStore`} for usage.
\r
25 angular.module('ngCookies', ['ng']).
\r
31 * Provides read/write access to browser's cookies.
\r
33 * Only a simple Object is exposed and by adding or removing properties to/from this object, new
\r
34 * cookies are created/deleted at the end of current $eval.
\r
35 * The object's properties can only be strings.
\r
37 * Requires the {@link ngCookies `ngCookies`} module to be installed.
\r
42 * angular.module('cookiesExample', ['ngCookies'])
\r
43 * .controller('ExampleController', ['$cookies', function($cookies) {
\r
44 * // Retrieving a cookie
\r
45 * var favoriteCookie = $cookies.myFavorite;
\r
46 * // Setting a cookie
\r
47 * $cookies.myFavorite = 'oatmeal';
\r
51 factory('$cookies', ['$rootScope', '$browser', function ($rootScope, $browser) {
\r
56 copy = angular.copy,
\r
57 isUndefined = angular.isUndefined;
\r
59 //creates a poller fn that copies all cookies from the $browser to service & inits the service
\r
60 $browser.addPollFn(function() {
\r
61 var currentCookies = $browser.cookies();
\r
62 if (lastBrowserCookies != currentCookies) { //relies on browser.cookies() impl
\r
63 lastBrowserCookies = currentCookies;
\r
64 copy(currentCookies, lastCookies);
\r
65 copy(currentCookies, cookies);
\r
66 if (runEval) $rootScope.$apply();
\r
72 //at the end of each eval, push cookies
\r
73 //TODO: this should happen before the "delayed" watches fire, because if some cookies are not
\r
74 // strings or browser refuses to store some cookies, we update the model in the push fn.
\r
75 $rootScope.$watch(push);
\r
81 * Pushes all the cookies from the service to the browser and verifies if all cookies were
\r
90 //delete any cookies deleted in $cookies
\r
91 for (name in lastCookies) {
\r
92 if (isUndefined(cookies[name])) {
\r
93 $browser.cookies(name, undefined);
\r
97 //update all cookies updated in $cookies
\r
98 for(name in cookies) {
\r
99 value = cookies[name];
\r
100 if (!angular.isString(value)) {
\r
101 value = '' + value;
\r
102 cookies[name] = value;
\r
104 if (value !== lastCookies[name]) {
\r
105 $browser.cookies(name, value);
\r
110 //verify what was actually stored
\r
113 browserCookies = $browser.cookies();
\r
115 for (name in cookies) {
\r
116 if (cookies[name] !== browserCookies[name]) {
\r
117 //delete or reset all cookies that the browser dropped from $cookies
\r
118 if (isUndefined(browserCookies[name])) {
\r
119 delete cookies[name];
\r
121 cookies[name] = browserCookies[name];
\r
133 * @name $cookieStore
\r
134 * @requires $cookies
\r
137 * Provides a key-value (string-object) storage, that is backed by session cookies.
\r
138 * Objects put or retrieved from this storage are automatically serialized or
\r
139 * deserialized by angular's toJson/fromJson.
\r
141 * Requires the {@link ngCookies `ngCookies`} module to be installed.
\r
146 * angular.module('cookieStoreExample', ['ngCookies'])
\r
147 * .controller('ExampleController', ['$cookieStore', function($cookieStore) {
\r
149 * $cookieStore.put('myFavorite','oatmeal');
\r
151 * var favoriteCookie = $cookieStore.get('myFavorite');
\r
152 * // Removing a cookie
\r
153 * $cookieStore.remove('myFavorite');
\r
157 factory('$cookieStore', ['$cookies', function($cookies) {
\r
162 * @name $cookieStore#get
\r
165 * Returns the value of given cookie key
\r
167 * @param {string} key Id to use for lookup.
\r
168 * @returns {Object} Deserialized cookie value.
\r
170 get: function(key) {
\r
171 var value = $cookies[key];
\r
172 return value ? angular.fromJson(value) : value;
\r
177 * @name $cookieStore#put
\r
180 * Sets a value for given cookie key
\r
182 * @param {string} key Id for the `value`.
\r
183 * @param {Object} value Value to be stored.
\r
185 put: function(key, value) {
\r
186 $cookies[key] = angular.toJson(value);
\r
191 * @name $cookieStore#remove
\r
194 * Remove given cookie
\r
196 * @param {string} key Id of the key-value pair to delete.
\r
198 remove: function(key) {
\r
199 delete $cookies[key];
\r
206 })(window, window.angular);