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