Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / docs / source / CookieProvider.html
1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-state.CookieProvider-method-constructor'><span id='Ext-state.CookieProvider-cfg-secure'><span id='Ext-state.CookieProvider-cfg-domain'><span id='Ext-state.CookieProvider-cfg-expires'><span id='Ext-state.CookieProvider-cfg-path'><span id='Ext-state.CookieProvider'>/**
2 </span></span></span></span></span></span> * @class Ext.state.CookieProvider
3  * @extends Ext.state.Provider
4  * A Provider implementation which saves and retrieves state via cookies.
5  * The CookieProvider supports the usual cookie options, such as:
6  * &lt;ul&gt;
7  * &lt;li&gt;{@link #path}&lt;/li&gt;
8  * &lt;li&gt;{@link #expires}&lt;/li&gt;
9  * &lt;li&gt;{@link #domain}&lt;/li&gt;
10  * &lt;li&gt;{@link #secure}&lt;/li&gt;
11  * &lt;/ul&gt;
12  &lt;pre&gt;&lt;code&gt;
13    var cp = new Ext.state.CookieProvider({
14        path: &quot;/cgi-bin/&quot;,
15        expires: new Date(new Date().getTime()+(1000*60*60*24*30)), //30 days
16        domain: &quot;sencha.com&quot;
17    });
18    Ext.state.Manager.setProvider(cp);
19  &lt;/code&gt;&lt;/pre&gt;
20  
21  
22  * @cfg {String} path The path for which the cookie is active (defaults to root '/' which makes it active for all pages in the site)
23  * @cfg {Date} expires The cookie expiration date (defaults to 7 days from now)
24  * @cfg {String} domain The domain to save the cookie for.  Note that you cannot specify a different domain than
25  * your page is on, but you can specify a sub-domain, or simply the domain itself like 'sencha.com' to include
26  * all sub-domains if you need to access cookies across different sub-domains (defaults to null which uses the same
27  * domain the page is running on including the 'www' like 'www.sencha.com')
28  * @cfg {Boolean} secure True if the site is using SSL (defaults to false)
29  * @constructor
30  * Create a new CookieProvider
31  * @param {Object} config The configuration object
32  */
33 Ext.define('Ext.state.CookieProvider', {
34     extend: 'Ext.state.Provider',
35
36     constructor : function(config){
37         var me = this;
38         me.path = &quot;/&quot;;
39         me.expires = new Date(new Date().getTime()+(1000*60*60*24*7)); //7 days
40         me.domain = null;
41         me.secure = false;
42         me.callParent(arguments);
43         me.state = me.readCookies();
44     },
45     
46     // private
47     set : function(name, value){
48         var me = this;
49         
50         if(typeof value == &quot;undefined&quot; || value === null){
51             me.clear(name);
52             return;
53         }
54         me.setCookie(name, value);
55         me.callParent(arguments);
56     },
57
58     // private
59     clear : function(name){
60         this.clearCookie(name);
61         this.callParent(arguments);
62     },
63
64     // private
65     readCookies : function(){
66         var cookies = {},
67             c = document.cookie + &quot;;&quot;,
68             re = /\s?(.*?)=(.*?);/g,
69             prefix = this.prefix,
70             len = prefix.length,
71             matches,
72             name,
73             value;
74             
75         while((matches = re.exec(c)) != null){
76             name = matches[1];
77             value = matches[2];
78             if (name &amp;&amp; name.substring(0, len) == prefix){
79                 cookies[name.substr(len)] = this.decodeValue(value);
80             }
81         }
82         return cookies;
83     },
84
85     // private
86     setCookie : function(name, value){
87         var me = this;
88         
89         document.cookie = me.prefix + name + &quot;=&quot; + me.encodeValue(value) +
90            ((me.expires == null) ? &quot;&quot; : (&quot;; expires=&quot; + me.expires.toGMTString())) +
91            ((me.path == null) ? &quot;&quot; : (&quot;; path=&quot; + me.path)) +
92            ((me.domain == null) ? &quot;&quot; : (&quot;; domain=&quot; + me.domain)) +
93            ((me.secure == true) ? &quot;; secure&quot; : &quot;&quot;);
94     },
95
96     // private
97     clearCookie : function(name){
98         var me = this;
99         
100         document.cookie = me.prefix + name + &quot;=null; expires=Thu, 01-Jan-70 00:00:01 GMT&quot; +
101            ((me.path == null) ? &quot;&quot; : (&quot;; path=&quot; + me.path)) +
102            ((me.domain == null) ? &quot;&quot; : (&quot;; domain=&quot; + me.domain)) +
103            ((me.secure == true) ? &quot;; secure&quot; : &quot;&quot;);
104     }
105 });
106 </pre></pre></body></html>