Upgrade to ExtJS 4.0.7 - Released 10/19/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 every 3 seconds.
63      */
64     interval: 3000,
65
66     /**
67      * @cfg {Object} baseParams
68      * An object containing properties which are to be sent as parameters on every polling request
69      */
70     
71     /**
72      * @cfg {String/Function} url
73      * The url which the PollingProvider should contact with each request. This can also be
74      * an imported Ext.Direct method which will accept the baseParams as its only argument.
75      */
76
77     // private
78     constructor : function(config){
79         this.callParent(arguments);
80         this.addEvents(
81             /**
82              * @event beforepoll
83              * Fired immediately before a poll takes place, an event handler can return false
84              * in order to cancel the poll.
85              * @param {Ext.direct.PollingProvider} this
86              */
87             'beforepoll',            
88             /**
89              * @event poll
90              * This event has not yet been implemented.
91              * @param {Ext.direct.PollingProvider} this
92              */
93             'poll'
94         );
95     },
96
97     // inherited
98     isConnected: function(){
99         return !!this.pollTask;
100     },
101
102     /**
103      * Connect to the server-side and begin the polling process. To handle each
104      * response subscribe to the data event.
105      */
106     connect: function(){
107         var me = this, url = me.url;
108         
109         if (url && !me.pollTask) {
110             me.pollTask = Ext.TaskManager.start({
111                 run: function(){
112                     if (me.fireEvent('beforepoll', me) !== false) {
113                         if (Ext.isFunction(url)) {
114                             url(me.baseParams);
115                         } else {
116                             Ext.Ajax.request({
117                                 url: url,
118                                 callback: me.onData,
119                                 scope: me,
120                                 params: me.baseParams
121                             });
122                         }
123                     }
124                 },
125                 interval: me.interval,
126                 scope: me
127             });
128             me.fireEvent('connect', me);
129         } else if (!url) {
130             //<debug>
131             Ext.Error.raise('Error initializing PollingProvider, no url configured.');
132             //</debug>
133         }
134     },
135
136     /**
137      * Disconnect from the server-side and stop the polling process. The disconnect
138      * event will be fired on a successful disconnect.
139      */
140     disconnect: function(){
141         var me = this;
142         
143         if (me.pollTask) {
144             Ext.TaskManager.stop(me.pollTask);
145             delete me.pollTask;
146             me.fireEvent('disconnect', me);
147         }
148     },
149
150     // private
151     onData: function(opt, success, response){
152         var me = this, 
153             i = 0, 
154             len,
155             events;
156         
157         if (success) {
158             events = me.createEvents(response);
159             for (len = events.length; i < len; ++i) {
160                 me.fireEvent('data', me, events[i]);
161             }
162         } else {
163             me.fireEvent('data', me, Ext.create('Ext.direct.ExceptionEvent', {
164                 data: null,
165                 code: Ext.direct.Manager.self.exceptions.TRANSPORT,
166                 message: 'Unable to connect to the server.',
167                 xhr: response
168             }));
169         }
170     }
171 });