Upgrade to ExtJS 3.1.0 - Released 12/16/2009
[extjs.git] / docs / source / PollingProvider.html
1 <html>\r
2 <head>\r
3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    \r
4   <title>The source code</title>\r
5     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />\r
6     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>\r
7 </head>\r
8 <body  onload="prettyPrint();">\r
9     <pre class="prettyprint lang-js"><div id="cls-Ext.direct.PollingProvider"></div>/**\r
10  * @class Ext.direct.PollingProvider\r
11  * @extends Ext.direct.JsonProvider\r
12  *\r
13  * <p>Provides for repetitive polling of the server at distinct {@link #interval intervals}.\r
14  * The initial request for data originates from the client, and then is responded to by the\r
15  * server.</p>\r
16  * \r
17  * <p>All configurations for the PollingProvider should be generated by the server-side\r
18  * API portion of the Ext.Direct stack.</p>\r
19  *\r
20  * <p>An instance of PollingProvider may be created directly via the new keyword or by simply\r
21  * specifying <tt>type = 'polling'</tt>.  For example:</p>\r
22  * <pre><code>\r
23 var pollA = new Ext.direct.PollingProvider({\r
24     type:'polling',\r
25     url: 'php/pollA.php',\r
26 });\r
27 Ext.Direct.addProvider(pollA);\r
28 pollA.disconnect();\r
29 \r
30 Ext.Direct.addProvider(\r
31     {\r
32         type:'polling',\r
33         url: 'php/pollB.php',\r
34         id: 'pollB-provider'\r
35     }\r
36 );\r
37 var pollB = Ext.Direct.getProvider('pollB-provider');\r
38  * </code></pre>\r
39  */\r
40 Ext.direct.PollingProvider = Ext.extend(Ext.direct.JsonProvider, {\r
41     <div id="cfg-Ext.direct.PollingProvider-priority"></div>/**\r
42      * @cfg {Number} priority\r
43      * Priority of the request (defaults to <tt>3</tt>). See {@link Ext.direct.Provider#priority}.\r
44      */\r
45     // override default priority\r
46     priority: 3,\r
47     \r
48     <div id="cfg-Ext.direct.PollingProvider-interval"></div>/**\r
49      * @cfg {Number} interval\r
50      * How often to poll the server-side in milliseconds (defaults to <tt>3000</tt> - every\r
51      * 3 seconds).\r
52      */\r
53     interval: 3000,\r
54 \r
55     <div id="cfg-Ext.direct.PollingProvider-baseParams"></div>/**\r
56      * @cfg {Object} baseParams An object containing properties which are to be sent as parameters\r
57      * on every polling request\r
58      */\r
59     \r
60     <div id="cfg-Ext.direct.PollingProvider-url"></div>/**\r
61      * @cfg {String/Function} url\r
62      * The url which the PollingProvider should contact with each request. This can also be\r
63      * an imported Ext.Direct method which will accept the baseParams as its only argument.\r
64      */\r
65 \r
66     // private\r
67     constructor : function(config){\r
68         Ext.direct.PollingProvider.superclass.constructor.call(this, config);\r
69         this.addEvents(\r
70             <div id="event-Ext.direct.PollingProvider-beforepoll"></div>/**\r
71              * @event beforepoll\r
72              * Fired immediately before a poll takes place, an event handler can return false\r
73              * in order to cancel the poll.\r
74              * @param {Ext.direct.PollingProvider}\r
75              */\r
76             'beforepoll',            \r
77             <div id="event-Ext.direct.PollingProvider-poll"></div>/**\r
78              * @event poll\r
79              * This event has not yet been implemented.\r
80              * @param {Ext.direct.PollingProvider}\r
81              */\r
82             'poll'\r
83         );\r
84     },\r
85 \r
86     // inherited\r
87     isConnected: function(){\r
88         return !!this.pollTask;\r
89     },\r
90 \r
91     <div id="method-Ext.direct.PollingProvider-connect"></div>/**\r
92      * Connect to the server-side and begin the polling process. To handle each\r
93      * response subscribe to the data event.\r
94      */\r
95     connect: function(){\r
96         if(this.url && !this.pollTask){\r
97             this.pollTask = Ext.TaskMgr.start({\r
98                 run: function(){\r
99                     if(this.fireEvent('beforepoll', this) !== false){\r
100                         if(typeof this.url == 'function'){\r
101                             this.url(this.baseParams);\r
102                         }else{\r
103                             Ext.Ajax.request({\r
104                                 url: this.url,\r
105                                 callback: this.onData,\r
106                                 scope: this,\r
107                                 params: this.baseParams\r
108                             });\r
109                         }\r
110                     }\r
111                 },\r
112                 interval: this.interval,\r
113                 scope: this\r
114             });\r
115             this.fireEvent('connect', this);\r
116         }else if(!this.url){\r
117             throw 'Error initializing PollingProvider, no url configured.';\r
118         }\r
119     },\r
120 \r
121     <div id="method-Ext.direct.PollingProvider-disconnect"></div>/**\r
122      * Disconnect from the server-side and stop the polling process. The disconnect\r
123      * event will be fired on a successful disconnect.\r
124      */\r
125     disconnect: function(){\r
126         if(this.pollTask){\r
127             Ext.TaskMgr.stop(this.pollTask);\r
128             delete this.pollTask;\r
129             this.fireEvent('disconnect', this);\r
130         }\r
131     },\r
132 \r
133     // private\r
134     onData: function(opt, success, xhr){\r
135         if(success){\r
136             var events = this.getEvents(xhr);\r
137             for(var i = 0, len = events.length; i < len; i++){\r
138                 var e = events[i];\r
139                 this.fireEvent('data', this, e);\r
140             }\r
141         }else{\r
142             var e = new Ext.Direct.ExceptionEvent({\r
143                 data: e,\r
144                 code: Ext.Direct.exceptions.TRANSPORT,\r
145                 message: 'Unable to connect to the server.',\r
146                 xhr: xhr\r
147             });\r
148             this.fireEvent('data', this, e);\r
149         }\r
150     }\r
151 });\r
152 \r
153 Ext.Direct.PROVIDERS['polling'] = Ext.direct.PollingProvider;</pre>    \r
154 </body>\r
155 </html>