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 <tt>3000</tt> - every
68 * @cfg {Object} baseParams An object containing properties which are to be sent as parameters
69 * on every polling request
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.
79 constructor : function(config){
80 this.callParent(arguments);
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}
91 * This event has not yet been implemented.
92 * @param {Ext.direct.PollingProvider}
99 isConnected: function(){
100 return !!this.pollTask;
104 * Connect to the server-side and begin the polling process. To handle each
105 * response subscribe to the data event.
108 var me = this, url = me.url;
110 if (url && !me.pollTask) {
111 me.pollTask = Ext.TaskManager.start({
113 if (me.fireEvent('beforepoll', me) !== false) {
114 if (Ext.isFunction(url)) {
121 params: me.baseParams
126 interval: me.interval,
129 me.fireEvent('connect', me);
132 Ext.Error.raise('Error initializing PollingProvider, no url configured.');
138 * Disconnect from the server-side and stop the polling process. The disconnect
139 * event will be fired on a successful disconnect.
141 disconnect: function(){
145 Ext.TaskManager.stop(me.pollTask);
147 me.fireEvent('disconnect', me);
152 onData: function(opt, success, response){
159 events = me.createEvents(response);
160 for (len = events.length; i < len; ++i) {
161 me.fireEvent('data', me, events[i]);
164 me.fireEvent('data', me, Ext.create('Ext.direct.ExceptionEvent', {
166 code: Ext.direct.Manager.self.exceptions.TRANSPORT,
167 message: 'Unable to connect to the server.',