3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
14 <div id="cls-Ext.direct.PollingProvider"></div>/**
\r
15 * @class Ext.direct.PollingProvider
\r
16 * @extends Ext.direct.JsonProvider
\r
18 * <p>Provides for repetitive polling of the server at distinct {@link #interval intervals}.
\r
19 * The initial request for data originates from the client, and then is responded to by the
\r
22 * <p>All configurations for the PollingProvider should be generated by the server-side
\r
23 * API portion of the Ext.Direct stack.</p>
\r
25 * <p>An instance of PollingProvider may be created directly via the new keyword or by simply
\r
26 * specifying <tt>type = 'polling'</tt>. For example:</p>
\r
28 var pollA = new Ext.direct.PollingProvider({
\r
30 url: 'php/pollA.php',
\r
32 Ext.Direct.addProvider(pollA);
\r
35 Ext.Direct.addProvider(
\r
38 url: 'php/pollB.php',
\r
39 id: 'pollB-provider'
\r
42 var pollB = Ext.Direct.getProvider('pollB-provider');
\r
45 Ext.direct.PollingProvider = Ext.extend(Ext.direct.JsonProvider, {
\r
46 <div id="cfg-Ext.direct.PollingProvider-priority"></div>/**
\r
47 * @cfg {Number} priority
\r
48 * Priority of the request (defaults to <tt>3</tt>). See {@link Ext.direct.Provider#priority}.
\r
50 // override default priority
\r
53 <div id="cfg-Ext.direct.PollingProvider-interval"></div>/**
\r
54 * @cfg {Number} interval
\r
55 * How often to poll the server-side in milliseconds (defaults to <tt>3000</tt> - every
\r
60 <div id="cfg-Ext.direct.PollingProvider-baseParams"></div>/**
\r
61 * @cfg {Object} baseParams An object containing properties which are to be sent as parameters
\r
62 * on every polling request
\r
65 <div id="cfg-Ext.direct.PollingProvider-url"></div>/**
\r
66 * @cfg {String/Function} url
\r
67 * The url which the PollingProvider should contact with each request. This can also be
\r
68 * an imported Ext.Direct method which will accept the baseParams as its only argument.
\r
72 constructor : function(config){
\r
73 Ext.direct.PollingProvider.superclass.constructor.call(this, config);
\r
75 <div id="event-Ext.direct.PollingProvider-beforepoll"></div>/**
\r
77 * Fired immediately before a poll takes place, an event handler can return false
\r
78 * in order to cancel the poll.
\r
79 * @param {Ext.direct.PollingProvider}
\r
82 <div id="event-Ext.direct.PollingProvider-poll"></div>/**
\r
84 * This event has not yet been implemented.
\r
85 * @param {Ext.direct.PollingProvider}
\r
92 isConnected: function(){
\r
93 return !!this.pollTask;
\r
96 <div id="method-Ext.direct.PollingProvider-connect"></div>/**
\r
97 * Connect to the server-side and begin the polling process. To handle each
\r
98 * response subscribe to the data event.
\r
100 connect: function(){
\r
101 if(this.url && !this.pollTask){
\r
102 this.pollTask = Ext.TaskMgr.start({
\r
104 if(this.fireEvent('beforepoll', this) !== false){
\r
105 if(typeof this.url == 'function'){
\r
106 this.url(this.baseParams);
\r
110 callback: this.onData,
\r
112 params: this.baseParams
\r
117 interval: this.interval,
\r
120 this.fireEvent('connect', this);
\r
121 }else if(!this.url){
\r
122 throw 'Error initializing PollingProvider, no url configured.';
\r
126 <div id="method-Ext.direct.PollingProvider-disconnect"></div>/**
\r
127 * Disconnect from the server-side and stop the polling process. The disconnect
\r
128 * event will be fired on a successful disconnect.
\r
130 disconnect: function(){
\r
132 Ext.TaskMgr.stop(this.pollTask);
\r
133 delete this.pollTask;
\r
134 this.fireEvent('disconnect', this);
\r
139 onData: function(opt, success, xhr){
\r
141 var events = this.getEvents(xhr);
\r
142 for(var i = 0, len = events.length; i < len; i++){
\r
144 this.fireEvent('data', this, e);
\r
147 var e = new Ext.Direct.ExceptionEvent({
\r
149 code: Ext.Direct.exceptions.TRANSPORT,
\r
150 message: 'Unable to connect to the server.',
\r
153 this.fireEvent('data', this, e);
\r
158 Ext.Direct.PROVIDERS['polling'] = Ext.direct.PollingProvider;</pre>