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 * A class used to load remote content to an Element. Sample usage:
18 * Ext.get('el').load({
26 * In general this class will not be instanced directly, rather the {@link Ext.Element#load} method
29 Ext.define('Ext.ElementLoader', {
31 /* Begin Definitions */
34 observable: 'Ext.util.Observable'
38 'Ext.data.Connection',
44 Html: function(loader, response, active){
45 loader.getTarget().update(response.responseText, active.scripts === true);
55 * The url to retrieve the content from.
60 * @cfg {Object} params
61 * Any params to be attached to the Ajax request. These parameters will
62 * be overridden by any params in the load options.
67 * @cfg {Object} baseParams Params that will be attached to every request. These parameters
68 * will not be overridden by any params in the load options.
73 * @cfg {Boolean/Object} autoLoad
74 * True to have the loader make a request as soon as it is created.
75 * This argument can also be a set of options that will be passed to {@link #load} is called.
80 * @cfg {HTMLElement/Ext.Element/String} target
81 * The target element for the loader. It can be the DOM element, the id or an {@link Ext.Element}.
86 * @cfg {Boolean/String} loadMask
87 * True or a string to show when the element is loading.
92 * @cfg {Object} ajaxOptions
93 * Any additional options to be passed to the request, for example timeout or headers.
98 * @cfg {Boolean} scripts
99 * True to parse any inline script tags in the response.
104 * @cfg {Function} success
105 * A function to be called when a load request is successful.
106 * Will be called with the following config parameters:
108 * - this - The ElementLoader instance.
109 * - response - The response object.
110 * - options - Ajax options.
114 * @cfg {Function} failure A function to be called when a load request fails.
115 * Will be called with the following config parameters:
117 * - this - The ElementLoader instance.
118 * - response - The response object.
119 * - options - Ajax options.
123 * @cfg {Function} callback A function to be called when a load request finishes.
124 * Will be called with the following config parameters:
126 * - this - The ElementLoader instance.
127 * - success - True if successful request.
128 * - response - The response object.
129 * - options - Ajax options.
133 * @cfg {Object} scope
134 * The scope to execute the {@link #success} and {@link #failure} functions in.
138 * @cfg {Function} renderer
139 * A custom function to render the content to the element. The passed parameters are:
143 * - The active request
148 constructor: function(config) {
152 config = config || {};
153 Ext.apply(me, config);
154 me.setTarget(me.target);
158 * Fires before a load request is made to the server.
159 * Returning false from an event listener can prevent the load
161 * @param {Ext.ElementLoader} this
162 * @param {Object} options The options passed to the request
168 * Fires after an unsuccessful load.
169 * @param {Ext.ElementLoader} this
170 * @param {Object} response The response from the server
171 * @param {Object} options The options passed to the request
177 * Fires after a successful load.
178 * @param {Ext.ElementLoader} this
179 * @param {Object} response The response from the server
180 * @param {Object} options The options passed to the request
185 // don't pass config because we have already applied it.
186 me.mixins.observable.constructor.call(me);
189 autoLoad = me.autoLoad;
190 if (autoLoad === true) {
198 * Sets an {@link Ext.Element} as the target of this loader.
199 * Note that if the target is changed, any active requests will be aborted.
200 * @param {String/HTMLElement/Ext.Element} target The element or its ID.
202 setTarget: function(target){
204 target = Ext.get(target);
205 if (me.target && me.target != target) {
212 * Returns the target of this loader.
213 * @return {Ext.Component} The target or null if none exists.
215 getTarget: function(){
216 return this.target || null;
220 * Aborts the active load request
223 var active = this.active;
224 if (active !== undefined) {
225 Ext.Ajax.abort(active.request);
234 * Removes the mask on the target
237 removeMask: function(){
238 this.target.unmask();
242 * Adds the mask on the target
244 * @param {Boolean/Object} mask The mask configuration
246 addMask: function(mask){
247 this.target.mask(mask === true ? null : mask);
251 * Loads new data from the server.
252 * @param {Object} options The options for the request. They can be any configuration option that can be specified for
253 * the class, with the exception of the target option. Note that any options passed to the method will override any
256 load: function(options) {
259 Ext.Error.raise('A valid target is required when loading content');
263 options = Ext.apply({}, options);
267 mask = Ext.isDefined(options.loadMask) ? options.loadMask : me.loadMask,
268 params = Ext.apply({}, options.params),
269 ajaxOptions = Ext.apply({}, options.ajaxOptions),
270 callback = options.callback || me.callback,
271 scope = options.scope || me.scope || me,
274 Ext.applyIf(ajaxOptions, me.ajaxOptions);
275 Ext.applyIf(options, ajaxOptions);
277 Ext.applyIf(params, me.params);
278 Ext.apply(params, me.baseParams);
280 Ext.applyIf(options, {
286 Ext.Error.raise('You must specify the URL from which content should be loaded');
293 callback: me.onComplete
296 if (me.fireEvent('beforeload', me, options) === false) {
304 request = Ext.Ajax.request(options);
311 success: options.success || me.success,
312 failure: options.failure || me.failure,
313 renderer: options.renderer || me.renderer,
314 scripts: Ext.isDefined(options.scripts) ? options.scripts : me.scripts
316 me.setOptions(me.active, options);
320 * Sets any additional options on the active request
322 * @param {Object} active The active request
323 * @param {Object} options The initial options
325 setOptions: Ext.emptyFn,
328 * Parses the response after the request completes
330 * @param {Object} options Ajax options
331 * @param {Boolean} success Success status of the request
332 * @param {Object} response The response object
334 onComplete: function(options, success, response) {
337 scope = active.scope,
338 renderer = me.getRenderer(active.renderer);
342 success = renderer.call(me, me, response, active);
346 Ext.callback(active.success, scope, [me, response, options]);
347 me.fireEvent('load', me, response, options);
349 Ext.callback(active.failure, scope, [me, response, options]);
350 me.fireEvent('exception', me, response, options);
352 Ext.callback(active.callback, scope, [me, success, response, options]);
362 * Gets the renderer to use
364 * @param {String/Function} renderer The renderer to use
365 * @return {Function} A rendering function to use.
367 getRenderer: function(renderer){
368 if (Ext.isFunction(renderer)) {
371 return this.statics().Renderer.Html;
375 * Automatically refreshes the content over a specified period.
376 * @param {Number} interval The interval to refresh in ms.
377 * @param {Object} options (optional) The options to pass to the load method. See {@link #load}
379 startAutoRefresh: function(interval, options){
381 me.stopAutoRefresh();
382 me.autoRefresh = setInterval(function(){
388 * Clears any auto refresh. See {@link #startAutoRefresh}.
390 stopAutoRefresh: function(){
391 clearInterval(this.autoRefresh);
392 delete this.autoRefresh;
396 * Checks whether the loader is automatically refreshing. See {@link #startAutoRefresh}.
397 * @return {Boolean} True if the loader is automatically refreshing
399 isAutoRefreshing: function(){
400 return Ext.isDefined(this.autoRefresh);
404 * Destroys the loader. Any active requests will be aborted.
408 me.stopAutoRefresh();