3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.direct.PollingProvider
17 * @extends Ext.direct.JsonProvider
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
23 * <p>All configurations for the PollingProvider should be generated by the server-side
24 * API portion of the Ext.Direct stack.</p>
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>
29 var pollA = new Ext.direct.PollingProvider({
33 Ext.direct.Manager.addProvider(pollA);
36 Ext.direct.Manager.addProvider(
43 var pollB = Ext.direct.Manager.getProvider('pollB-provider');
46 Ext.define('Ext.direct.PollingProvider', {
48 /* Begin Definitions */
50 extend: 'Ext.direct.JsonProvider',
52 alias: 'direct.pollingprovider',
54 uses: ['Ext.direct.ExceptionEvent'],
56 requires: ['Ext.Ajax', 'Ext.util.DelayedTask'],
61 * @cfg {Number} interval
62 * How often to poll the server-side in milliseconds. Defaults to every 3 seconds.
67 * @cfg {Object} baseParams
68 * An object containing properties which are to be sent as parameters on every polling request
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.
78 constructor : function(config){
79 this.callParent(arguments);
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
90 * This event has not yet been implemented.
91 * @param {Ext.direct.PollingProvider} this
98 isConnected: function(){
99 return !!this.pollTask;
103 * Connect to the server-side and begin the polling process. To handle each
104 * response subscribe to the data event.
107 var me = this, url = me.url;
109 if (url && !me.pollTask) {
110 me.pollTask = Ext.TaskManager.start({
112 if (me.fireEvent('beforepoll', me) !== false) {
113 if (Ext.isFunction(url)) {
120 params: me.baseParams
125 interval: me.interval,
128 me.fireEvent('connect', me);
131 Ext.Error.raise('Error initializing PollingProvider, no url configured.');
137 * Disconnect from the server-side and stop the polling process. The disconnect
138 * event will be fired on a successful disconnect.
140 disconnect: function(){
144 Ext.TaskManager.stop(me.pollTask);
146 me.fireEvent('disconnect', me);
151 onData: function(opt, success, response){
158 events = me.createEvents(response);
159 for (len = events.length; i < len; ++i) {
160 me.fireEvent('data', me, events[i]);
163 me.fireEvent('data', me, Ext.create('Ext.direct.ExceptionEvent', {
165 code: Ext.direct.Manager.self.exceptions.TRANSPORT,
166 message: 'Unable to connect to the server.',