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