3 * Copyright(c) 2006-2010 Ext JS, Inc.
5 * http://www.extjs.com/license
8 * @class Ext.direct.PollingProvider
9 * @extends Ext.direct.JsonProvider
11 * <p>Provides for repetitive polling of the server at distinct {@link #interval intervals}.
12 * The initial request for data originates from the client, and then is responded to by the
15 * <p>All configurations for the PollingProvider should be generated by the server-side
16 * API portion of the Ext.Direct stack.</p>
18 * <p>An instance of PollingProvider may be created directly via the new keyword or by simply
19 * specifying <tt>type = 'polling'</tt>. For example:</p>
21 var pollA = new Ext.direct.PollingProvider({
25 Ext.Direct.addProvider(pollA);
28 Ext.Direct.addProvider(
35 var pollB = Ext.Direct.getProvider('pollB-provider');
38 Ext.direct.PollingProvider = Ext.extend(Ext.direct.JsonProvider, {
40 * @cfg {Number} priority
41 * Priority of the request (defaults to <tt>3</tt>). See {@link Ext.direct.Provider#priority}.
43 // override default priority
47 * @cfg {Number} interval
48 * How often to poll the server-side in milliseconds (defaults to <tt>3000</tt> - every
54 * @cfg {Object} baseParams An object containing properties which are to be sent as parameters
55 * on every polling request
59 * @cfg {String/Function} url
60 * The url which the PollingProvider should contact with each request. This can also be
61 * an imported Ext.Direct method which will accept the baseParams as its only argument.
65 constructor : function(config){
66 Ext.direct.PollingProvider.superclass.constructor.call(this, config);
70 * Fired immediately before a poll takes place, an event handler can return false
71 * in order to cancel the poll.
72 * @param {Ext.direct.PollingProvider}
77 * This event has not yet been implemented.
78 * @param {Ext.direct.PollingProvider}
85 isConnected: function(){
86 return !!this.pollTask;
90 * Connect to the server-side and begin the polling process. To handle each
91 * response subscribe to the data event.
94 if(this.url && !this.pollTask){
95 this.pollTask = Ext.TaskMgr.start({
97 if(this.fireEvent('beforepoll', this) !== false){
98 if(typeof this.url == 'function'){
99 this.url(this.baseParams);
103 callback: this.onData,
105 params: this.baseParams
110 interval: this.interval,
113 this.fireEvent('connect', this);
115 throw 'Error initializing PollingProvider, no url configured.';
120 * Disconnect from the server-side and stop the polling process. The disconnect
121 * event will be fired on a successful disconnect.
123 disconnect: function(){
125 Ext.TaskMgr.stop(this.pollTask);
126 delete this.pollTask;
127 this.fireEvent('disconnect', this);
132 onData: function(opt, success, xhr){
134 var events = this.getEvents(xhr);
135 for(var i = 0, len = events.length; i < len; i++){
137 this.fireEvent('data', this, e);
140 var e = new Ext.Direct.ExceptionEvent({
142 code: Ext.Direct.exceptions.TRANSPORT,
143 message: 'Unable to connect to the server.',
146 this.fireEvent('data', this, e);
151 Ext.Direct.PROVIDERS['polling'] = Ext.direct.PollingProvider;