Upgrade to ExtJS 3.2.0 - Released 03/30/2010
[extjs.git] / docs / source / PollingProvider.html
1 <html>
2 <head>
3   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    
4   <title>The source code</title>
5     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
6     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 </head>
8 <body  onload="prettyPrint();">
9     <pre class="prettyprint lang-js">/*!
10  * Ext JS Library 3.2.0
11  * Copyright(c) 2006-2010 Ext JS, Inc.
12  * licensing@extjs.com
13  * http://www.extjs.com/license
14  */
15 <div id="cls-Ext.direct.PollingProvider"></div>/**
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.addProvider(pollA);
34 pollA.disconnect();
35
36 Ext.Direct.addProvider(
37     {
38         type:'polling',
39         url: 'php/pollB.php',
40         id: 'pollB-provider'
41     }
42 );
43 var pollB = Ext.Direct.getProvider('pollB-provider');
44  * </code></pre>
45  */
46 Ext.direct.PollingProvider = Ext.extend(Ext.direct.JsonProvider, {
47     <div id="cfg-Ext.direct.PollingProvider-priority"></div>/**
48      * @cfg {Number} priority
49      * Priority of the request (defaults to <tt>3</tt>). See {@link Ext.direct.Provider#priority}.
50      */
51     // override default priority
52     priority: 3,
53     
54     <div id="cfg-Ext.direct.PollingProvider-interval"></div>/**
55      * @cfg {Number} interval
56      * How often to poll the server-side in milliseconds (defaults to <tt>3000</tt> - every
57      * 3 seconds).
58      */
59     interval: 3000,
60
61     <div id="cfg-Ext.direct.PollingProvider-baseParams"></div>/**
62      * @cfg {Object} baseParams An object containing properties which are to be sent as parameters
63      * on every polling request
64      */
65     
66     <div id="cfg-Ext.direct.PollingProvider-url"></div>/**
67      * @cfg {String/Function} url
68      * The url which the PollingProvider should contact with each request. This can also be
69      * an imported Ext.Direct method which will accept the baseParams as its only argument.
70      */
71
72     // private
73     constructor : function(config){
74         Ext.direct.PollingProvider.superclass.constructor.call(this, config);
75         this.addEvents(
76             <div id="event-Ext.direct.PollingProvider-beforepoll"></div>/**
77              * @event beforepoll
78              * Fired immediately before a poll takes place, an event handler can return false
79              * in order to cancel the poll.
80              * @param {Ext.direct.PollingProvider}
81              */
82             'beforepoll',            
83             <div id="event-Ext.direct.PollingProvider-poll"></div>/**
84              * @event poll
85              * This event has not yet been implemented.
86              * @param {Ext.direct.PollingProvider}
87              */
88             'poll'
89         );
90     },
91
92     // inherited
93     isConnected: function(){
94         return !!this.pollTask;
95     },
96
97     <div id="method-Ext.direct.PollingProvider-connect"></div>/**
98      * Connect to the server-side and begin the polling process. To handle each
99      * response subscribe to the data event.
100      */
101     connect: function(){
102         if(this.url && !this.pollTask){
103             this.pollTask = Ext.TaskMgr.start({
104                 run: function(){
105                     if(this.fireEvent('beforepoll', this) !== false){
106                         if(typeof this.url == 'function'){
107                             this.url(this.baseParams);
108                         }else{
109                             Ext.Ajax.request({
110                                 url: this.url,
111                                 callback: this.onData,
112                                 scope: this,
113                                 params: this.baseParams
114                             });
115                         }
116                     }
117                 },
118                 interval: this.interval,
119                 scope: this
120             });
121             this.fireEvent('connect', this);
122         }else if(!this.url){
123             throw 'Error initializing PollingProvider, no url configured.';
124         }
125     },
126
127     <div id="method-Ext.direct.PollingProvider-disconnect"></div>/**
128      * Disconnect from the server-side and stop the polling process. The disconnect
129      * event will be fired on a successful disconnect.
130      */
131     disconnect: function(){
132         if(this.pollTask){
133             Ext.TaskMgr.stop(this.pollTask);
134             delete this.pollTask;
135             this.fireEvent('disconnect', this);
136         }
137     },
138
139     // private
140     onData: function(opt, success, xhr){
141         if(success){
142             var events = this.getEvents(xhr);
143             for(var i = 0, len = events.length; i < len; i++){
144                 var e = events[i];
145                 this.fireEvent('data', this, e);
146             }
147         }else{
148             var e = new Ext.Direct.ExceptionEvent({
149                 data: e,
150                 code: Ext.Direct.exceptions.TRANSPORT,
151                 message: 'Unable to connect to the server.',
152                 xhr: xhr
153             });
154             this.fireEvent('data', this, e);
155         }
156     }
157 });
158
159 Ext.Direct.PROVIDERS['polling'] = Ext.direct.PollingProvider;</pre>    
160 </body>
161 </html>