3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
16 * @class Ext.ElementLoader
17 * A class used to load remote content to an Element. Sample usage:
28 * In general this class will not be instanced directly, rather the {@link Ext.core.Element#load} method
32 Ext.define('Ext.ElementLoader', {
34 /* Begin Definitions */
37 observable: 'Ext.util.Observable'
41 'Ext.data.Connection',
47 Html: function(loader, response, active){
48 loader.getTarget().update(response.responseText, active.scripts === true);
57 * @cfg {String} url The url to retrieve the content from. Defaults to <tt>null</tt>.
62 * @cfg {Object} params Any params to be attached to the Ajax request. These parameters will
63 * be overridden by any params in the load options. Defaults to <tt>null</tt>.
68 * @cfg {Object} baseParams Params that will be attached to every request. These parameters
69 * will not be overridden by any params in the load options. Defaults to <tt>null</tt>.
74 * @cfg {Boolean/Object} autoLoad True to have the loader make a request as soon as it is created. Defaults to <tt>false</tt>.
75 * This argument can also be a set of options that will be passed to {@link #load} is called.
80 * @cfg {Mixed} target The target element for the loader. It can be the DOM element, the id or an Ext.Element.
85 * @cfg {Mixed} loadMask True or a string to show when the element is loading.
90 * @cfg {Object} ajaxOptions Any additional options to be passed to the request, for example timeout or headers. Defaults to <tt>null</tt>.
95 * @cfg {Boolean} scripts True to parse any inline script tags in the response.
100 * @cfg {Function} success A function to be called when a load request is successful.
104 * @cfg {Function} failure A function to be called when a load request fails.
108 * @cfg {Object} scope The scope to execute the {@link #success} and {@link #failure} functions in.
112 * @cfg {Function} renderer A custom function to render the content to the element. The passed parameters
115 * <li>The loader</li>
116 * <li>The response</li>
117 * <li>The active request</li>
123 constructor: function(config) {
127 config = config || {};
128 Ext.apply(me, config);
129 me.setTarget(me.target);
133 * Fires before a load request is made to the server.
134 * Returning false from an event listener can prevent the load
136 * @param {Ext.ElementLoader} this
137 * @param {Object} options The options passed to the request
143 * Fires after an unsuccessful load.
144 * @param {Ext.ElementLoader} this
145 * @param {Object} response The response from the server
146 * @param {Object} options The options passed to the request
152 * Fires after a successful load.
153 * @param {Ext.ElementLoader} this
154 * @param {Object} response The response from the server
155 * @param {Object} options The options passed to the request
160 // don't pass config because we have already applied it.
161 me.mixins.observable.constructor.call(me);
164 autoLoad = me.autoLoad;
165 if (autoLoad === true) {
173 * Set an {Ext.Element} as the target of this loader. Note that if the target is changed,
174 * any active requests will be aborted.
175 * @param {Mixed} target The element
177 setTarget: function(target){
179 target = Ext.get(target);
180 if (me.target && me.target != target) {
187 * Get the target of this loader.
188 * @return {Ext.Component} target The target, null if none exists.
190 getTarget: function(){
191 return this.target || null;
195 * Aborts the active load request
198 var active = this.active;
199 if (active !== undefined) {
200 Ext.Ajax.abort(active.request);
209 * Remove the mask on the target
212 removeMask: function(){
213 this.target.unmask();
217 * Add the mask on the target
219 * @param {Mixed} mask The mask configuration
221 addMask: function(mask){
222 this.target.mask(mask === true ? null : mask);
226 * Load new data from the server.
227 * @param {Object} options The options for the request. They can be any configuration option that can be specified for
228 * the class, with the exception of the target option. Note that any options passed to the method will override any
231 load: function(options) {
234 Ext.Error.raise('A valid target is required when loading content');
238 options = Ext.apply({}, options);
242 mask = Ext.isDefined(options.loadMask) ? options.loadMask : me.loadMask,
243 params = Ext.apply({}, options.params),
244 ajaxOptions = Ext.apply({}, options.ajaxOptions),
245 callback = options.callback || me.callback,
246 scope = options.scope || me.scope || me,
249 Ext.applyIf(ajaxOptions, me.ajaxOptions);
250 Ext.applyIf(options, ajaxOptions);
252 Ext.applyIf(params, me.params);
253 Ext.apply(params, me.baseParams);
255 Ext.applyIf(options, {
261 Ext.Error.raise('You must specify the URL from which content should be loaded');
268 callback: me.onComplete
271 if (me.fireEvent('beforeload', me, options) === false) {
279 request = Ext.Ajax.request(options);
286 success: options.success || me.success,
287 failure: options.failure || me.failure,
288 renderer: options.renderer || me.renderer,
289 scripts: Ext.isDefined(options.scripts) ? options.scripts : me.scripts
291 me.setOptions(me.active, options);
295 * Set any additional options on the active request
297 * @param {Object} active The active request
298 * @param {Object} options The initial options
300 setOptions: Ext.emptyFn,
303 * Parse the response after the request completes
305 * @param {Object} options Ajax options
306 * @param {Boolean} success Success status of the request
307 * @param {Object} response The response object
309 onComplete: function(options, success, response) {
312 scope = active.scope,
313 renderer = me.getRenderer(active.renderer);
317 success = renderer.call(me, me, response, active);
321 Ext.callback(active.success, scope, [me, response, options]);
322 me.fireEvent('load', me, response, options);
324 Ext.callback(active.failure, scope, [me, response, options]);
325 me.fireEvent('exception', me, response, options);
327 Ext.callback(active.callback, scope, [me, success, response, options]);
337 * Gets the renderer to use
339 * @param {String/Function} renderer The renderer to use
340 * @return {Function} A rendering function to use.
342 getRenderer: function(renderer){
343 if (Ext.isFunction(renderer)) {
346 return this.statics().Renderer.Html;
350 * Automatically refreshes the content over a specified period.
351 * @param {Number} interval The interval to refresh in ms.
352 * @param {Object} options (optional) The options to pass to the load method. See {@link #load}
354 startAutoRefresh: function(interval, options){
356 me.stopAutoRefresh();
357 me.autoRefresh = setInterval(function(){
363 * Clears any auto refresh. See {@link #startAutoRefresh}.
365 stopAutoRefresh: function(){
366 clearInterval(this.autoRefresh);
367 delete this.autoRefresh;
371 * Checks whether the loader is automatically refreshing. See {@link #startAutoRefresh}.
372 * @return {Boolean} True if the loader is automatically refreshing
374 isAutoRefreshing: function(){
375 return Ext.isDefined(this.autoRefresh);
379 * Destroys the loader. Any active requests will be aborted.
383 me.stopAutoRefresh();