3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.Updater"></div>/**
11 * @extends Ext.util.Observable
12 * Provides AJAX-style update capabilities for Element objects. Updater can be used to {@link #update}
13 * an {@link Ext.Element} once, or you can use {@link #startAutoRefresh} to set up an auto-updating
14 * {@link Ext.Element Element} on a specific interval.<br><br>
17 * var el = Ext.get("foo"); // Get Ext.Element object
18 * var mgr = el.getUpdater();
20 url: "http://myserver.com/index.php",
27 * mgr.formUpdate("myFormId", "http://myserver.com/index.php");
29 * // or directly (returns the same Updater instance)
30 * var mgr = new Ext.Updater("myElementId");
31 * mgr.startAutoRefresh(60, "http://myserver.com/index.php");
32 * mgr.on("update", myFcnNeedsToKnow);
34 * // short handed call directly from the element object
35 * Ext.get("foo").load({
38 params: "param1=foo&param2=bar",
39 text: "Loading Foo..."
43 * Create new Updater directly.
44 * @param {Mixed} el The element to update
45 * @param {Boolean} forceNew (optional) By default the constructor checks to see if the passed element already
46 * has an Updater and if it does it returns the same instance. This will skip that check (useful for extending this class).
48 Ext.UpdateManager = Ext.Updater = Ext.extend(Ext.util.Observable,
50 var BEFOREUPDATE = "beforeupdate",
55 function processSuccess(response){
57 me.transaction = null;
58 if (response.argument.form && response.argument.reset) {
59 try { // put in try/catch since some older FF releases had problems with this
60 response.argument.form.reset();
64 me.renderer.render(me.el, response, me,
65 updateComplete.createDelegate(me, [response]));
67 me.renderer.render(me.el, response, me);
68 updateComplete.call(me, response);
73 function updateComplete(response, type, success){
74 this.fireEvent(type || UPDATE, this.el, response);
75 if(Ext.isFunction(response.argument.callback)){
76 response.argument.callback.call(response.argument.scope, this.el, Ext.isEmpty(success) ? true : false, response, response.argument.options);
81 function processFailure(response){
82 updateComplete.call(this, response, FAILURE, !!(this.transaction = null));
86 constructor: function(el, forceNew){
89 if(!forceNew && el.updateManager){
90 return el.updateManager;
92 <div id="prop-Ext.Updater-el"></div>/**
97 <div id="prop-Ext.Updater-defaultUrl"></div>/**
98 * Cached url to use for refreshes. Overwritten every time update() is called unless "discardUrl" param is set to true.
101 me.defaultUrl = null;
104 <div id="event-Ext.Updater-beforeupdate"></div>/**
105 * @event beforeupdate
106 * Fired before an update is made, return false from your handler and the update is cancelled.
107 * @param {Ext.Element} el
108 * @param {String/Object/Function} url
109 * @param {String/Object} params
112 <div id="event-Ext.Updater-update"></div>/**
114 * Fired after successful update is made.
115 * @param {Ext.Element} el
116 * @param {Object} oResponseObject The response Object
119 <div id="event-Ext.Updater-failure"></div>/**
121 * Fired on update failure.
122 * @param {Ext.Element} el
123 * @param {Object} oResponseObject The response Object
128 Ext.apply(me, Ext.Updater.defaults);
129 <div id="prop-Ext.Updater-sslBlankUrl"></div>/**
130 * Blank page URL to use with SSL file uploads (defaults to {@link Ext.Updater.defaults#sslBlankUrl}).
131 * @property sslBlankUrl
134 <div id="prop-Ext.Updater-disableCaching"></div>/**
135 * Whether to append unique parameter on get request to disable caching (defaults to {@link Ext.Updater.defaults#disableCaching}).
136 * @property disableCaching
139 <div id="prop-Ext.Updater-indicatorText"></div>/**
140 * Text for loading indicator (defaults to {@link Ext.Updater.defaults#indicatorText}).
141 * @property indicatorText
144 <div id="prop-Ext.Updater-showLoadIndicator"></div>/**
145 * Whether to show indicatorText when loading (defaults to {@link Ext.Updater.defaults#showLoadIndicator}).
146 * @property showLoadIndicator
149 <div id="prop-Ext.Updater-timeout"></div>/**
150 * Timeout for requests or form posts in seconds (defaults to {@link Ext.Updater.defaults#timeout}).
154 <div id="prop-Ext.Updater-loadScripts"></div>/**
155 * True to process scripts in the output (defaults to {@link Ext.Updater.defaults#loadScripts}).
156 * @property loadScripts
160 <div id="prop-Ext.Updater-transaction"></div>/**
161 * Transaction object of the current executing transaction, or null if there is no active transaction.
163 me.transaction = null;
164 <div id="prop-Ext.Updater-refreshDelegate"></div>/**
165 * Delegate for refresh() prebound to "this", use myUpdater.refreshDelegate.createCallback(arg1, arg2) to bind arguments
168 me.refreshDelegate = me.refresh.createDelegate(me);
169 <div id="prop-Ext.Updater-updateDelegate"></div>/**
170 * Delegate for update() prebound to "this", use myUpdater.updateDelegate.createCallback(arg1, arg2) to bind arguments
173 me.updateDelegate = me.update.createDelegate(me);
174 <div id="prop-Ext.Updater-formUpdateDelegate"></div>/**
175 * Delegate for formUpdate() prebound to "this", use myUpdater.formUpdateDelegate.createCallback(arg1, arg2) to bind arguments
178 me.formUpdateDelegate = (me.formUpdate || function(){}).createDelegate(me);
180 <div id="prop-Ext.Updater-renderer"></div>/**
181 * The renderer for this Updater (defaults to {@link Ext.Updater.BasicRenderer}).
183 me.renderer = me.renderer || me.getDefaultRenderer();
185 Ext.Updater.superclass.constructor.call(me);
188 <div id="method-Ext.Updater-setRenderer"></div>/**
189 * Sets the content renderer for this Updater. See {@link Ext.Updater.BasicRenderer#render} for more details.
190 * @param {Object} renderer The object implementing the render() method
192 setRenderer : function(renderer){
193 this.renderer = renderer;
196 <div id="method-Ext.Updater-getRenderer"></div>/**
197 * Returns the current content renderer for this Updater. See {@link Ext.Updater.BasicRenderer#render} for more details.
200 getRenderer : function(){
201 return this.renderer;
204 <div id="method-Ext.Updater-getDefaultRenderer"></div>/**
205 * This is an overrideable method which returns a reference to a default
206 * renderer class if none is specified when creating the Ext.Updater.
207 * Defaults to {@link Ext.Updater.BasicRenderer}
209 getDefaultRenderer: function() {
210 return new Ext.Updater.BasicRenderer();
213 <div id="method-Ext.Updater-setDefaultUrl"></div>/**
214 * Sets the default URL used for updates.
215 * @param {String/Function} defaultUrl The url or a function to call to get the url
217 setDefaultUrl : function(defaultUrl){
218 this.defaultUrl = defaultUrl;
221 <div id="method-Ext.Updater-getEl"></div>/**
222 * Get the Element this Updater is bound to
223 * @return {Ext.Element} The element
229 <div id="method-Ext.Updater-update"></div>/**
230 * Performs an <b>asynchronous</b> request, updating this element with the response.
231 * If params are specified it uses POST, otherwise it uses GET.<br><br>
232 * <b>Note:</b> Due to the asynchronous nature of remote server requests, the Element
233 * will not have been fully updated when the function returns. To post-process the returned
234 * data, use the callback option, or an <b><code>update</code></b> event handler.
235 * @param {Object} options A config object containing any of the following options:<ul>
236 * <li>url : <b>String/Function</b><p class="sub-desc">The URL to request or a function which
237 * <i>returns</i> the URL (defaults to the value of {@link Ext.Ajax#url} if not specified).</p></li>
238 * <li>method : <b>String</b><p class="sub-desc">The HTTP method to
239 * use. Defaults to POST if the <code>params</code> argument is present, otherwise GET.</p></li>
240 * <li>params : <b>String/Object/Function</b><p class="sub-desc">The
241 * parameters to pass to the server (defaults to none). These may be specified as a url-encoded
242 * string, or as an object containing properties which represent parameters,
243 * or as a function, which returns such an object.</p></li>
244 * <li>scripts : <b>Boolean</b><p class="sub-desc">If <code>true</code>
245 * any <script> tags embedded in the response text will be extracted
246 * and executed (defaults to {@link Ext.Updater.defaults#loadScripts}). If this option is specified,
247 * the callback will be called <i>after</i> the execution of the scripts.</p></li>
248 * <li>callback : <b>Function</b><p class="sub-desc">A function to
249 * be called when the response from the server arrives. The following
250 * parameters are passed:<ul>
251 * <li><b>el</b> : Ext.Element<p class="sub-desc">The Element being updated.</p></li>
252 * <li><b>success</b> : Boolean<p class="sub-desc">True for success, false for failure.</p></li>
253 * <li><b>response</b> : XMLHttpRequest<p class="sub-desc">The XMLHttpRequest which processed the update.</p></li>
254 * <li><b>options</b> : Object<p class="sub-desc">The config object passed to the update call.</p></li></ul>
256 * <li>scope : <b>Object</b><p class="sub-desc">The scope in which
257 * to execute the callback (The callback's <code>this</code> reference.) If the
258 * <code>params</code> argument is a function, this scope is used for that function also.</p></li>
259 * <li>discardUrl : <b>Boolean</b><p class="sub-desc">By default, the URL of this request becomes
260 * the default URL for this Updater object, and will be subsequently used in {@link #refresh}
261 * calls. To bypass this behavior, pass <code>discardUrl:true</code> (defaults to false).</p></li>
262 * <li>timeout : <b>Number</b><p class="sub-desc">The number of seconds to wait for a response before
263 * timing out (defaults to {@link Ext.Updater.defaults#timeout}).</p></li>
264 * <li>text : <b>String</b><p class="sub-desc">The text to use as the innerHTML of the
265 * {@link Ext.Updater.defaults#indicatorText} div (defaults to 'Loading...'). To replace the entire div, not
266 * just the text, override {@link Ext.Updater.defaults#indicatorText} directly.</p></li>
267 * <li>nocache : <b>Boolean</b><p class="sub-desc">Only needed for GET
268 * requests, this option causes an extra, auto-generated parameter to be appended to the request
269 * to defeat caching (defaults to {@link Ext.Updater.defaults#disableCaching}).</p></li></ul>
275 params: {param1: "foo", param2: "bar"}, // or a URL encoded string
276 callback: yourFunction,
277 scope: yourObject, //(optional scope)
282 scripts: false // Save time by avoiding RegExp execution.
286 update : function(url, params, callback, discardUrl){
291 if(me.fireEvent(BEFOREUPDATE, me.el, url, params) !== false){
292 if(Ext.isObject(url)){ // must be config object
295 params = params || cfg.params;
296 callback = callback || cfg.callback;
297 discardUrl = discardUrl || cfg.discardUrl;
298 callerScope = cfg.scope;
299 if(!Ext.isEmpty(cfg.nocache)){me.disableCaching = cfg.nocache;};
300 if(!Ext.isEmpty(cfg.text)){me.indicatorText = '<div class="loading-indicator">'+cfg.text+"</div>";};
301 if(!Ext.isEmpty(cfg.scripts)){me.loadScripts = cfg.scripts;};
302 if(!Ext.isEmpty(cfg.timeout)){me.timeout = cfg.timeout;};
309 if(Ext.isFunction(url)){
313 var o = Ext.apply({}, {
315 params: (Ext.isFunction(params) && callerScope) ? params.createDelegate(callerScope) : params,
316 success: processSuccess,
317 failure: processFailure,
320 timeout: (me.timeout*1000),
321 disableCaching: me.disableCaching,
326 "callback": callback,
327 "scope": callerScope || window,
332 me.transaction = Ext.Ajax.request(o);
336 <div id="method-Ext.Updater-formUpdate"></div>/**
337 * <p>Performs an asynchronous form post, updating this element with the response. If the form has the attribute
338 * enctype="<a href="http://www.faqs.org/rfcs/rfc2388.html">multipart/form-data</a>", it assumes it's a file upload.
339 * Uses this.sslBlankUrl for SSL file uploads to prevent IE security warning.</p>
340 * <p>File uploads are not performed using normal "Ajax" techniques, that is they are <b>not</b>
341 * performed using XMLHttpRequests. Instead the form is submitted in the standard manner with the
342 * DOM <code><form></code> element temporarily modified to have its
343 * <a href="http://www.w3.org/TR/REC-html40/present/frames.html#adef-target">target</a> set to refer
344 * to a dynamically generated, hidden <code><iframe></code> which is inserted into the document
345 * but removed after the return data has been gathered.</p>
346 * <p>Be aware that file upload packets, sent with the content type <a href="http://www.faqs.org/rfcs/rfc2388.html">multipart/form-data</a>
347 * and some server technologies (notably JEE) may require some custom processing in order to
348 * retrieve parameter names and parameter values from the packet content.</p>
349 * @param {String/HTMLElement} form The form Id or form element
350 * @param {String} url (optional) The url to pass the form to. If omitted the action attribute on the form will be used.
351 * @param {Boolean} reset (optional) Whether to try to reset the form after the update
352 * @param {Function} callback (optional) Callback when transaction is complete. The following
353 * parameters are passed:<ul>
354 * <li><b>el</b> : Ext.Element<p class="sub-desc">The Element being updated.</p></li>
355 * <li><b>success</b> : Boolean<p class="sub-desc">True for success, false for failure.</p></li>
356 * <li><b>response</b> : XMLHttpRequest<p class="sub-desc">The XMLHttpRequest which processed the update.</p></li></ul>
358 formUpdate : function(form, url, reset, callback){
360 if(me.fireEvent(BEFOREUPDATE, me.el, form, url) !== false){
361 if(Ext.isFunction(url)){
364 form = Ext.getDom(form)
365 me.transaction = Ext.Ajax.request({
368 success: processSuccess,
369 failure: processFailure,
371 timeout: (me.timeout*1000),
375 "callback": callback,
379 me.showLoading.defer(1, me);
383 <div id="method-Ext.Updater-startAutoRefresh"></div>/**
384 * Set this element to auto refresh. Can be canceled by calling {@link #stopAutoRefresh}.
385 * @param {Number} interval How often to update (in seconds).
386 * @param {String/Object/Function} url (optional) The url for this request, a config object in the same format
387 * supported by {@link #load}, or a function to call to get the url (defaults to the last used url). Note that while
388 * the url used in a load call can be reused by this method, other load config options will not be reused and must be
389 * sepcified as part of a config object passed as this paramter if needed.
390 * @param {String/Object} params (optional) The parameters to pass as either a url encoded string
391 * "¶m1=1¶m2=2" or as an object {param1: 1, param2: 2}
392 * @param {Function} callback (optional) Callback when transaction is complete - called with signature (oElement, bSuccess)
393 * @param {Boolean} refreshNow (optional) Whether to execute the refresh now, or wait the interval
395 startAutoRefresh : function(interval, url, params, callback, refreshNow){
398 me.update(url || me.defaultUrl, params, callback, true);
400 if(me.autoRefreshProcId){
401 clearInterval(me.autoRefreshProcId);
403 me.autoRefreshProcId = setInterval(me.update.createDelegate(me, [url || me.defaultUrl, params, callback, true]), interval * 1000);
406 <div id="method-Ext.Updater-stopAutoRefresh"></div>/**
407 * Stop auto refresh on this element.
409 stopAutoRefresh : function(){
410 if(this.autoRefreshProcId){
411 clearInterval(this.autoRefreshProcId);
412 delete this.autoRefreshProcId;
416 <div id="method-Ext.Updater-isAutoRefreshing"></div>/**
417 * Returns true if the Updater is currently set to auto refresh its content (see {@link #startAutoRefresh}), otherwise false.
419 isAutoRefreshing : function(){
420 return !!this.autoRefreshProcId;
423 <div id="method-Ext.Updater-showLoading"></div>/**
424 * Display the element's "loading" state. By default, the element is updated with {@link #indicatorText}. This
425 * method may be overridden to perform a custom action while this Updater is actively updating its contents.
427 showLoading : function(){
428 if(this.showLoadIndicator){
429 this.el.dom.innerHTML = this.indicatorText;
433 <div id="method-Ext.Updater-abort"></div>/**
434 * Aborts the currently executing transaction, if any.
437 if(this.transaction){
438 Ext.Ajax.abort(this.transaction);
442 <div id="method-Ext.Updater-isUpdating"></div>/**
443 * Returns true if an update is in progress, otherwise false.
446 isUpdating : function(){
447 return this.transaction ? Ext.Ajax.isLoading(this.transaction) : false;
450 <div id="method-Ext.Updater-refresh"></div>/**
451 * Refresh the element with the last used url or defaultUrl. If there is no url, it returns immediately
452 * @param {Function} callback (optional) Callback when transaction is complete - called with signature (oElement, bSuccess)
454 refresh : function(callback){
456 this.update(this.defaultUrl, null, callback, true);
462 <div id="cls-Ext.Updater.defaults"></div>/**
463 * @class Ext.Updater.defaults
464 * The defaults collection enables customizing the default properties of Updater
466 Ext.Updater.defaults = {
467 <div id="prop-Ext.Updater.defaults-timeout"></div>/**
468 * Timeout for requests or form posts in seconds (defaults to 30 seconds).
472 <div id="prop-Ext.Updater.defaults-disableCaching"></div>/**
473 * True to append a unique parameter to GET requests to disable caching (defaults to false).
476 disableCaching : false,
477 <div id="prop-Ext.Updater.defaults-showLoadIndicator"></div>/**
478 * Whether or not to show {@link #indicatorText} during loading (defaults to true).
481 showLoadIndicator : true,
482 <div id="prop-Ext.Updater.defaults-indicatorText"></div>/**
483 * Text for loading indicator (defaults to '<div class="loading-indicator">Loading...</div>').
486 indicatorText : '<div class="loading-indicator">Loading...</div>',
487 <div id="prop-Ext.Updater.defaults-loadScripts"></div>/**
488 * True to process scripts by default (defaults to false).
492 <div id="prop-Ext.Updater.defaults-sslBlankUrl"></div>/**
493 * Blank page URL to use with SSL file uploads (defaults to {@link Ext#SSL_SECURE_URL} if set, or "javascript:false").
496 sslBlankUrl : Ext.SSL_SECURE_URL
500 <div id="method-Ext.Updater-Updater.updateElement"></div>/**
501 * Static convenience method. <b>This method is deprecated in favor of el.load({url:'foo.php', ...})</b>.
503 * <pre><code>Ext.Updater.updateElement("my-div", "stuff.php");</code></pre>
504 * @param {Mixed} el The element to update
505 * @param {String} url The url
506 * @param {String/Object} params (optional) Url encoded param string or an object of name/value pairs
507 * @param {Object} options (optional) A config object with any of the Updater properties you want to set - for
508 * example: {disableCaching:true, indicatorText: "Loading data..."}
511 * @member Ext.Updater
513 Ext.Updater.updateElement = function(el, url, params, options){
514 var um = Ext.get(el).getUpdater();
515 Ext.apply(um, options);
516 um.update(url, params, options ? options.callback : null);
519 <div id="cls-Ext.Updater.BasicRenderer"></div>/**
520 * @class Ext.Updater.BasicRenderer
521 * <p>This class is a base class implementing a simple render method which updates an element using results from an Ajax request.</p>
522 * <p>The BasicRenderer updates the element's innerHTML with the responseText. To perform a custom render (i.e. XML or JSON processing),
523 * create an object with a conforming {@link #render} method and pass it to setRenderer on the Updater.</p>
525 Ext.Updater.BasicRenderer = function(){};
527 Ext.Updater.BasicRenderer.prototype = {
528 <div id="method-Ext.Updater.BasicRenderer-render"></div>/**
529 * This method is called when an Ajax response is received, and an Element needs updating.
530 * @param {Ext.Element} el The element being rendered
531 * @param {Object} xhr The XMLHttpRequest object
532 * @param {Updater} updateManager The calling update manager
533 * @param {Function} callback A callback that will need to be called if loadScripts is true on the Updater
535 render : function(el, response, updateManager, callback){
536 el.update(response.responseText, updateManager.loadScripts, callback);