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