PollingProvider.html
5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The source code</title>
<link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
<script type="text/javascript" src="../resources/prettify/prettify.js"></script>
<style type="text/css">
.highlight { display: block; background-color: #ddd; }
</style>
<script type="text/javascript">
function highlight() {
document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
}
</script>
</head>
<body onload="prettyPrint(); highlight();">
<pre class="prettyprint lang-js"><span id='Ext-direct-PollingProvider'>/**
</span> * @class Ext.direct.PollingProvider
*
* <p>Provides for repetitive polling of the server at distinct {@link #interval intervals}.
* The initial request for data originates from the client, and then is responded to by the
* server.</p>
*
* <p>All configurations for the PollingProvider should be generated by the server-side
* API portion of the Ext.Direct stack.</p>
*
* <p>An instance of PollingProvider may be created directly via the new keyword or by simply
* specifying <tt>type = 'polling'</tt>. For example:</p>
* <pre><code>
var pollA = new Ext.direct.PollingProvider({
type:'polling',
url: 'php/pollA.php',
});
Ext.direct.Manager.addProvider(pollA);
pollA.disconnect();
Ext.direct.Manager.addProvider(
{
type:'polling',
url: 'php/pollB.php',
id: 'pollB-provider'
}
);
var pollB = Ext.direct.Manager.getProvider('pollB-provider');
* </code></pre>
*/
Ext.define('Ext.direct.PollingProvider', {
/* Begin Definitions */
extend: 'Ext.direct.JsonProvider',
alias: 'direct.pollingprovider',
uses: ['Ext.direct.ExceptionEvent'],
requires: ['Ext.Ajax', 'Ext.util.DelayedTask'],
/* End Definitions */
<span id='Ext-direct-PollingProvider-cfg-interval'> /**
</span> * @cfg {Number} interval
* How often to poll the server-side in milliseconds. Defaults to every 3 seconds.
*/
interval: 3000,
<span id='Ext-direct-PollingProvider-cfg-baseParams'> /**
</span> * @cfg {Object} baseParams
* An object containing properties which are to be sent as parameters on every polling request
*/
<span id='Ext-direct-PollingProvider-cfg-url'> /**
</span> * @cfg {String/Function} url
* The url which the PollingProvider should contact with each request. This can also be
* an imported Ext.Direct method which will accept the baseParams as its only argument.
*/
// private
constructor : function(config){
this.callParent(arguments);
this.addEvents(
<span id='Ext-direct-PollingProvider-event-beforepoll'> /**
</span> * @event beforepoll
* Fired immediately before a poll takes place, an event handler can return false
* in order to cancel the poll.
* @param {Ext.direct.PollingProvider} this
*/
'beforepoll',
<span id='Ext-direct-PollingProvider-event-poll'> /**
</span> * @event poll
* This event has not yet been implemented.
* @param {Ext.direct.PollingProvider} this
*/
'poll'
);
},
// inherited
isConnected: function(){
return !!this.pollTask;
},
<span id='Ext-direct-PollingProvider-method-connect'> /**
</span> * Connect to the server-side and begin the polling process. To handle each
* response subscribe to the data event.
*/
connect: function(){
var me = this, url = me.url;
if (url && !me.pollTask) {
me.pollTask = Ext.TaskManager.start({
run: function(){
if (me.fireEvent('beforepoll', me) !== false) {
if (Ext.isFunction(url)) {
url(me.baseParams);
} else {
Ext.Ajax.request({
url: url,
callback: me.onData,
scope: me,
params: me.baseParams
});
}
}
},
interval: me.interval,
scope: me
});
me.fireEvent('connect', me);
} else if (!url) {
//<debug>
Ext.Error.raise('Error initializing PollingProvider, no url configured.');
//</debug>
}
},
<span id='Ext-direct-PollingProvider-method-disconnect'> /**
</span> * Disconnect from the server-side and stop the polling process. The disconnect
* event will be fired on a successful disconnect.
*/
disconnect: function(){
var me = this;
if (me.pollTask) {
Ext.TaskManager.stop(me.pollTask);
delete me.pollTask;
me.fireEvent('disconnect', me);
}
},
// private
onData: function(opt, success, response){
var me = this,
i = 0,
len,
events;
if (success) {
events = me.createEvents(response);
for (len = events.length; i < len; ++i) {
me.fireEvent('data', me, events[i]);
}
} else {
me.fireEvent('data', me, new Ext.direct.ExceptionEvent({
data: null,
code: Ext.direct.Manager.exceptions.TRANSPORT,
message: 'Unable to connect to the server.',
xhr: response
}));
}
}
});</pre>
</body>
</html>