19
19
20
20
angular . module ( 'ngCookies' , [ 'ng' ] ) .
21
21
/**
22
- * @ngdoc service
23
- * @name $cookies
24
- *
22
+ * @ngdoc provider
23
+ * @name $cookiesProvider
25
24
* @description
26
- * Provides read/write access to browser's cookies.
27
- *
28
- * BREAKING CHANGE: `$cookies` no longer exposes properties that represent the
29
- * current browser cookie values. Now you must use the get/put/remove/etc. methods
30
- * as described below.
31
- *
32
- * Requires the {@link ngCookies `ngCookies`} module to be installed.
33
- *
34
- * @example
35
- *
36
- * ```js
37
- * angular.module('cookiesExample', ['ngCookies'])
38
- * .controller('ExampleController', ['$cookies', function($cookies) {
39
- * // Retrieving a cookie
40
- * var favoriteCookie = $cookies.get('myFavorite');
41
- * // Setting a cookie
42
- * $cookies.put('myFavorite', 'oatmeal');
43
- * }]);
44
- * ```
45
- */
46
- factory ( '$cookies' , [ '$$cookieReader' , '$$cookieWriter' , function ( $$cookieReader , $$cookieWriter ) {
25
+ * Use `$cookiesProvider` to change the default behavior of the {@link ngCookies.$cookies $cookies} service.
26
+ * */
27
+ provider ( '$cookies' , [ function $CookiesProvider ( ) {
28
+ /**
29
+ * @ngdoc property
30
+ * @name $cookiesProvider#defaults
31
+ * @description
32
+ *
33
+ * Object containing default options to pass when setting cookies.
34
+ *
35
+ * The object may have following properties:
36
+ *
37
+ * - **path** - `{string}` - The cookie will be available only for this path and its
38
+ * sub-paths. By default, this would be the URL that appears in your base tag.
39
+ * - **domain** - `{string}` - The cookie will be available only for this domain and
40
+ * its sub-domains. For obvious security reasons the user agent will not accept the
41
+ * cookie if the current domain is not a sub domain or equals to the requested domain.
42
+ * - **expires** - `{string|Date}` - String of the form "Wdy, DD Mon YYYY HH:MM:SS GMT"
43
+ * or a Date object indicating the exact date/time this cookie will expire.
44
+ * - **secure** - `{boolean}` - The cookie will be available only in secured connection.
45
+ *
46
+ * Note: by default the address that appears in your <base> tag will be used as path.
47
+ * This is import so that cookies will be visible for all routes in case html5mode is enabled
48
+ *
49
+ **/
50
+ var defaults = this . defaults = { } ;
51
+
52
+ function calcOptions ( options ) {
53
+ return options ? angular . extend ( { } , defaults , options ) : defaults ;
54
+ }
55
+
56
+ /**
57
+ * @ngdoc service
58
+ * @name $cookies
59
+ *
60
+ * @description
61
+ * Provides read/write access to browser's cookies.
62
+ *
63
+ * BREAKING CHANGE: `$cookies` no longer exposes properties that represent the
64
+ * current browser cookie values. Now you must use the get/put/remove/etc. methods
65
+ * as described below.
66
+ *
67
+ * Requires the {@link ngCookies `ngCookies`} module to be installed.
68
+ *
69
+ * @example
70
+ *
71
+ * ```js
72
+ * angular.module('cookiesExample', ['ngCookies'])
73
+ * .controller('ExampleController', ['$cookies', function($cookies) {
74
+ * // Retrieving a cookie
75
+ * var favoriteCookie = $cookies.get('myFavorite');
76
+ * // Setting a cookie
77
+ * $cookies.put('myFavorite', 'oatmeal');
78
+ * }]);
79
+ * ```
80
+ */
81
+ this . $get = [ '$$cookieReader' , '$$cookieWriter' , function ( $$cookieReader , $$cookieWriter ) {
47
82
return {
48
83
/**
49
84
* @ngdoc method
@@ -70,7 +105,7 @@ angular.module('ngCookies', ['ng']).
70
105
* @returns {Object } Deserialized cookie value.
71
106
*/
72
107
getObject : function ( key ) {
73
- var value = $$cookieReader ( ) [ key ] ;
108
+ var value = this . get ( key ) ;
74
109
return value ? angular . fromJson ( value ) : value ;
75
110
} ,
76
111
@@ -96,20 +131,11 @@ angular.module('ngCookies', ['ng']).
96
131
*
97
132
* @param {string } key Id for the `value`.
98
133
* @param {string } value Raw value to be stored.
99
- * @param {Object= } options Object with options that need to be stored for the cookie.
100
- * The object may have following properties:
101
- *
102
- * - **path** - `{string}` - The cookie will be available only for this path and its
103
- * sub-paths. By default, this would be the URL that appears in your base tag.
104
- * - **domain** - `{string}` - The cookie will be available only for this domain and
105
- * its sub-domains. For obvious security reasons the user agent will not accept the
106
- * cookie if the current domain is not a sub domain or equals to the requested domain.
107
- * - **expires** - `{string|Date}` - String of the form "Wdy, DD Mon YYYY HH:MM:SS GMT"
108
- * or a Date object indicating the exact date/time this cookie will expire.
109
- * - **secure** - `{boolean}` - The cookie will be available only in secured connection.
134
+ * @param {Object= } options Options object.
135
+ * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
110
136
*/
111
137
put : function ( key , value , options ) {
112
- $$cookieWriter ( key , value , options ) ;
138
+ $$cookieWriter ( key , value , calcOptions ( options ) ) ;
113
139
} ,
114
140
115
141
/**
@@ -122,9 +148,10 @@ angular.module('ngCookies', ['ng']).
122
148
* @param {string } key Id for the `value`.
123
149
* @param {Object } value Value to be stored.
124
150
* @param {Object= } options Options object.
151
+ * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
125
152
*/
126
153
putObject : function ( key , value , options ) {
127
- $$cookieWriter ( key , angular . toJson ( value ) , options ) ;
154
+ this . put ( key , angular . toJson ( value ) , options ) ;
128
155
} ,
129
156
130
157
/**
@@ -136,9 +163,11 @@ angular.module('ngCookies', ['ng']).
136
163
*
137
164
* @param {string } key Id of the key-value pair to delete.
138
165
* @param {Object= } options Options object.
166
+ * See {@link ngCookies.$cookiesProvider#defaults $cookiesProvider.defaults}
139
167
*/
140
168
remove : function ( key , options ) {
141
- $$cookieWriter ( key , undefined , options ) ;
169
+ $$cookieWriter ( key , undefined , calcOptions ( options ) ) ;
142
170
}
143
171
} ;
144
- } ] ) ;
172
+ } ] ;
173
+ } ] ) ;
0 commit comments