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