Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / direct / PollingProvider.js
1 /*
2
3 This file is part of Ext JS 4
4
5 Copyright (c) 2011 Sencha Inc
6
7 Contact:  http://www.sencha.com/contact
8
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file.  Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
11
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
13
14 */
15 /**
16  * @class Ext.direct.PollingProvider
17  * @extends Ext.direct.JsonProvider
18  *
19  * <p>Provides for repetitive polling of the server at distinct {@link #interval intervals}.
20  * The initial request for data originates from the client, and then is responded to by the
21  * server.</p>
22  * 
23  * <p>All configurations for the PollingProvider should be generated by the server-side
24  * API portion of the Ext.Direct stack.</p>
25  *
26  * <p>An instance of PollingProvider may be created directly via the new keyword or by simply
27  * specifying <tt>type = 'polling'</tt>.  For example:</p>
28  * <pre><code>
29 var pollA = new Ext.direct.PollingProvider({
30     type:'polling',
31     url: 'php/pollA.php',
32 });
33 Ext.direct.Manager.addProvider(pollA);
34 pollA.disconnect();
35
36 Ext.direct.Manager.addProvider(
37     {
38         type:'polling',
39         url: 'php/pollB.php',
40         id: 'pollB-provider'
41     }
42 );
43 var pollB = Ext.direct.Manager.getProvider('pollB-provider');
44  * </code></pre>
45  */
46 Ext.define('Ext.direct.PollingProvider', {
47     
48     /* Begin Definitions */
49     
50     extend: 'Ext.direct.JsonProvider',
51     
52     alias: 'direct.pollingprovider',
53     
54     uses: ['Ext.direct.ExceptionEvent'],
55     
56     requires: ['Ext.Ajax', 'Ext.util.DelayedTask'],
57     
58     /* End Definitions */
59     
60     /**
61      * @cfg {Number} interval
62      * How often to poll the server-side in milliseconds (defaults to <tt>3000</tt> - every
63      * 3 seconds).
64      */
65     interval: 3000,
66
67     /**
68      * @cfg {Object} baseParams An object containing properties which are to be sent as parameters
69      * on every polling request
70      */
71     
72     /**
73      * @cfg {String/Function} url
74      * The url which the PollingProvider should contact with each request. This can also be
75      * an imported Ext.Direct method which will accept the baseParams as its only argument.
76      */
77
78     // private
79     constructor : function(config){
80         this.callParent(arguments);
81         this.addEvents(
82             /**
83              * @event beforepoll
84              * Fired immediately before a poll takes place, an event handler can return false
85              * in order to cancel the poll.
86              * @param {Ext.direct.PollingProvider}
87              */
88             'beforepoll',            
89             /**
90              * @event poll
91              * This event has not yet been implemented.
92              * @param {Ext.direct.PollingProvider}
93              */
94             'poll'
95         );
96     },
97
98     // inherited
99     isConnected: function(){
100         return !!this.pollTask;
101     },
102
103     /**
104      * Connect to the server-side and begin the polling process. To handle each
105      * response subscribe to the data event.
106      */
107     connect: function(){
108         var me = this, url = me.url;
109         
110         if (url && !me.pollTask) {
111             me.pollTask = Ext.TaskManager.start({
112                 run: function(){
113                     if (me.fireEvent('beforepoll', me) !== false) {
114                         if (Ext.isFunction(url)) {
115                             url(me.baseParams);
116                         } else {
117                             Ext.Ajax.request({
118                                 url: url,
119                                 callback: me.onData,
120                                 scope: me,
121                                 params: me.baseParams
122                             });
123                         }
124                     }
125                 },
126                 interval: me.interval,
127                 scope: me
128             });
129             me.fireEvent('connect', me);
130         } else if (!url) {
131             //<debug>
132             Ext.Error.raise('Error initializing PollingProvider, no url configured.');
133             //</debug>
134         }
135     },
136
137     /**
138      * Disconnect from the server-side and stop the polling process. The disconnect
139      * event will be fired on a successful disconnect.
140      */
141     disconnect: function(){
142         var me = this;
143         
144         if (me.pollTask) {
145             Ext.TaskManager.stop(me.pollTask);
146             delete me.pollTask;
147             me.fireEvent('disconnect', me);
148         }
149     },
150
151     // private
152     onData: function(opt, success, response){
153         var me = this, 
154             i = 0, 
155             len,
156             events;
157         
158         if (success) {
159             events = me.createEvents(response);
160             for (len = events.length; i < len; ++i) {
161                 me.fireEvent('data', me, events[i]);
162             }
163         } else {
164             me.fireEvent('data', me, Ext.create('Ext.direct.ExceptionEvent', {
165                 data: null,
166                 code: Ext.direct.Manager.self.exceptions.TRANSPORT,
167                 message: 'Unable to connect to the server.',
168                 xhr: response
169             }));
170         }
171     }
172 });