ElementLoader.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-ElementLoader'>/**
  19. </span> * A class used to load remote content to an Element. Sample usage:
  20. *
  21. * Ext.get('el').load({
  22. * url: 'myPage.php',
  23. * scripts: true,
  24. * params: {
  25. * id: 1
  26. * }
  27. * });
  28. *
  29. * In general this class will not be instanced directly, rather the {@link Ext.Element#method-load} method
  30. * will be used.
  31. */
  32. Ext.define('Ext.ElementLoader', {
  33. /* Begin Definitions */
  34. mixins: {
  35. observable: 'Ext.util.Observable'
  36. },
  37. uses: [
  38. 'Ext.data.Connection',
  39. 'Ext.Ajax'
  40. ],
  41. statics: {
  42. Renderer: {
  43. Html: function(loader, response, active){
  44. loader.getTarget().update(response.responseText, active.scripts === true);
  45. return true;
  46. }
  47. }
  48. },
  49. /* End Definitions */
  50. <span id='Ext-ElementLoader-cfg-url'> /**
  51. </span> * @cfg {String} url (required)
  52. * The url to retrieve the content from.
  53. */
  54. url: null,
  55. <span id='Ext-ElementLoader-cfg-params'> /**
  56. </span> * @cfg {Object} params
  57. * Any params to be attached to the Ajax request. These parameters will
  58. * be overridden by any params in the load options.
  59. */
  60. params: null,
  61. <span id='Ext-ElementLoader-cfg-baseParams'> /**
  62. </span> * @cfg {Object} baseParams Params that will be attached to every request. These parameters
  63. * will not be overridden by any params in the load options.
  64. */
  65. baseParams: null,
  66. <span id='Ext-ElementLoader-cfg-autoLoad'> /**
  67. </span> * @cfg {Boolean/Object} autoLoad
  68. * True to have the loader make a request as soon as it is created.
  69. * This argument can also be a set of options that will be passed to {@link #method-load} is called.
  70. */
  71. autoLoad: false,
  72. <span id='Ext-ElementLoader-cfg-target'> /**
  73. </span> * @cfg {HTMLElement/Ext.Element/String} target
  74. * The target element for the loader. It can be the DOM element, the id or an {@link Ext.Element}.
  75. */
  76. target: null,
  77. <span id='Ext-ElementLoader-cfg-loadMask'> /**
  78. </span> * @cfg {Boolean/String} loadMask
  79. * True or a string to show when the element is loading.
  80. */
  81. loadMask: false,
  82. <span id='Ext-ElementLoader-cfg-ajaxOptions'> /**
  83. </span> * @cfg {Object} ajaxOptions
  84. * Any additional options to be passed to the request, for example timeout or headers.
  85. */
  86. ajaxOptions: null,
  87. <span id='Ext-ElementLoader-cfg-scripts'> /**
  88. </span> * @cfg {Boolean} scripts
  89. * True to parse any inline script tags in the response.
  90. */
  91. scripts: false,
  92. <span id='Ext-ElementLoader-cfg-success'> /**
  93. </span> * @cfg {Function} success
  94. * A function to be called when a load request is successful.
  95. * Will be called with the following config parameters:
  96. *
  97. * - this - The ElementLoader instance.
  98. * - response - The response object.
  99. * - options - Ajax options.
  100. */
  101. <span id='Ext-ElementLoader-cfg-failure'> /**
  102. </span> * @cfg {Function} failure A function to be called when a load request fails.
  103. * Will be called with the following config parameters:
  104. *
  105. * - this - The ElementLoader instance.
  106. * - response - The response object.
  107. * - options - Ajax options.
  108. */
  109. <span id='Ext-ElementLoader-cfg-callback'> /**
  110. </span> * @cfg {Function} callback A function to be called when a load request finishes.
  111. * Will be called with the following config parameters:
  112. *
  113. * - this - The ElementLoader instance.
  114. * - success - True if successful request.
  115. * - response - The response object.
  116. * - options - Ajax options.
  117. */
  118. <span id='Ext-ElementLoader-cfg-scope'> /**
  119. </span> * @cfg {Object} scope
  120. * The scope to execute the {@link #success} and {@link #failure} functions in.
  121. */
  122. <span id='Ext-ElementLoader-cfg-renderer'> /**
  123. </span> * @cfg {Function} renderer
  124. * A custom function to render the content to the element. The function should
  125. * return false if the renderer could not be applied. The passed parameters are:
  126. *
  127. * - The loader
  128. * - The response
  129. * - The active request
  130. */
  131. <span id='Ext-ElementLoader-property-isLoader'> /**
  132. </span> * @property {Boolean} isLoader
  133. * `true` in this class to identify an object as an instantiated ElementLoader, or subclass thereof.
  134. */
  135. isLoader: true,
  136. constructor: function(config) {
  137. var me = this,
  138. autoLoad;
  139. config = config || {};
  140. Ext.apply(me, config);
  141. me.setTarget(me.target);
  142. me.addEvents(
  143. <span id='Ext-ElementLoader-event-beforeload'> /**
  144. </span> * @event beforeload
  145. * Fires before a load request is made to the server.
  146. * Returning false from an event listener can prevent the load
  147. * from occurring.
  148. * @param {Ext.ElementLoader} this
  149. * @param {Object} options The options passed to the request
  150. */
  151. 'beforeload',
  152. <span id='Ext-ElementLoader-event-exception'> /**
  153. </span> * @event exception
  154. * Fires after an unsuccessful load.
  155. * @param {Ext.ElementLoader} this
  156. * @param {Object} response The response from the server
  157. * @param {Object} options The options passed to the request
  158. */
  159. 'exception',
  160. <span id='Ext-ElementLoader-event-load'> /**
  161. </span> * @event load
  162. * Fires after a successful load.
  163. * @param {Ext.ElementLoader} this
  164. * @param {Object} response The response from the server
  165. * @param {Object} options The options passed to the request
  166. */
  167. 'load'
  168. );
  169. // don't pass config because we have already applied it.
  170. me.mixins.observable.constructor.call(me);
  171. if (me.autoLoad) {
  172. autoLoad = me.autoLoad;
  173. if (autoLoad === true) {
  174. autoLoad = {};
  175. }
  176. me.load(autoLoad);
  177. }
  178. },
  179. <span id='Ext-ElementLoader-method-setTarget'> /**
  180. </span> * Sets an {@link Ext.Element} as the target of this loader.
  181. * Note that if the target is changed, any active requests will be aborted.
  182. * @param {String/HTMLElement/Ext.Element} target The element or its ID.
  183. */
  184. setTarget: function(target){
  185. var me = this;
  186. target = Ext.get(target);
  187. if (me.target &amp;&amp; me.target != target) {
  188. me.abort();
  189. }
  190. me.target = target;
  191. },
  192. <span id='Ext-ElementLoader-method-getTarget'> /**
  193. </span> * Returns the target of this loader.
  194. * @return {Ext.Component} The target or null if none exists.
  195. */
  196. getTarget: function(){
  197. return this.target || null;
  198. },
  199. <span id='Ext-ElementLoader-method-abort'> /**
  200. </span> * Aborts the active load request
  201. */
  202. abort: function(){
  203. var active = this.active;
  204. if (active !== undefined) {
  205. Ext.Ajax.abort(active.request);
  206. if (active.mask) {
  207. this.removeMask();
  208. }
  209. delete this.active;
  210. }
  211. },
  212. <span id='Ext-ElementLoader-method-removeMask'> /**
  213. </span> * Removes the mask on the target
  214. * @private
  215. */
  216. removeMask: function(){
  217. this.target.unmask();
  218. },
  219. <span id='Ext-ElementLoader-method-addMask'> /**
  220. </span> * Adds the mask on the target
  221. * @private
  222. * @param {Boolean/Object} mask The mask configuration
  223. */
  224. addMask: function(mask){
  225. this.target.mask(mask === true ? null : mask);
  226. },
  227. <span id='Ext-ElementLoader-method-load'> /**
  228. </span> * Loads new data from the server.
  229. * @param {Object} options The options for the request. They can be any configuration option that can be specified for
  230. * the class, with the exception of the target option. Note that any options passed to the method will override any
  231. * class defaults.
  232. */
  233. load: function(options) {
  234. //&lt;debug&gt;
  235. if (!this.target) {
  236. Ext.Error.raise('A valid target is required when loading content');
  237. }
  238. //&lt;/debug&gt;
  239. options = Ext.apply({}, options);
  240. var me = this,
  241. target = me.target,
  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,
  247. request;
  248. Ext.applyIf(ajaxOptions, me.ajaxOptions);
  249. Ext.applyIf(options, ajaxOptions);
  250. Ext.applyIf(params, me.params);
  251. Ext.apply(params, me.baseParams);
  252. Ext.applyIf(options, {
  253. url: me.url
  254. });
  255. //&lt;debug&gt;
  256. if (!options.url) {
  257. Ext.Error.raise('You must specify the URL from which content should be loaded');
  258. }
  259. //&lt;/debug&gt;
  260. Ext.apply(options, {
  261. scope: me,
  262. params: params,
  263. callback: me.onComplete
  264. });
  265. if (me.fireEvent('beforeload', me, options) === false) {
  266. return;
  267. }
  268. if (mask) {
  269. me.addMask(mask);
  270. }
  271. request = Ext.Ajax.request(options);
  272. me.active = {
  273. request: request,
  274. options: options,
  275. mask: mask,
  276. scope: scope,
  277. callback: callback,
  278. success: options.success || me.success,
  279. failure: options.failure || me.failure,
  280. renderer: options.renderer || me.renderer,
  281. scripts: Ext.isDefined(options.scripts) ? options.scripts : me.scripts
  282. };
  283. me.setOptions(me.active, options);
  284. },
  285. <span id='Ext-ElementLoader-method-setOptions'> /**
  286. </span> * Sets any additional options on the active request
  287. * @private
  288. * @param {Object} active The active request
  289. * @param {Object} options The initial options
  290. */
  291. setOptions: Ext.emptyFn,
  292. <span id='Ext-ElementLoader-method-onComplete'> /**
  293. </span> * Parses the response after the request completes
  294. * @private
  295. * @param {Object} options Ajax options
  296. * @param {Boolean} success Success status of the request
  297. * @param {Object} response The response object
  298. */
  299. onComplete: function(options, success, response) {
  300. var me = this,
  301. active = me.active,
  302. scope = active.scope,
  303. renderer = me.getRenderer(active.renderer);
  304. if (success) {
  305. success = renderer.call(me, me, response, active) !== false;
  306. }
  307. if (success) {
  308. Ext.callback(active.success, scope, [me, response, options]);
  309. me.fireEvent('load', me, response, options);
  310. } else {
  311. Ext.callback(active.failure, scope, [me, response, options]);
  312. me.fireEvent('exception', me, response, options);
  313. }
  314. Ext.callback(active.callback, scope, [me, success, response, options]);
  315. if (active.mask) {
  316. me.removeMask();
  317. }
  318. delete me.active;
  319. },
  320. <span id='Ext-ElementLoader-method-getRenderer'> /**
  321. </span> * Gets the renderer to use
  322. * @private
  323. * @param {String/Function} renderer The renderer to use
  324. * @return {Function} A rendering function to use.
  325. */
  326. getRenderer: function(renderer){
  327. if (Ext.isFunction(renderer)) {
  328. return renderer;
  329. }
  330. return this.statics().Renderer.Html;
  331. },
  332. <span id='Ext-ElementLoader-method-startAutoRefresh'> /**
  333. </span> * Automatically refreshes the content over a specified period.
  334. * @param {Number} interval The interval to refresh in ms.
  335. * @param {Object} options (optional) The options to pass to the load method. See {@link #method-load}
  336. */
  337. startAutoRefresh: function(interval, options){
  338. var me = this;
  339. me.stopAutoRefresh();
  340. me.autoRefresh = setInterval(function(){
  341. me.load(options);
  342. }, interval);
  343. },
  344. <span id='Ext-ElementLoader-method-stopAutoRefresh'> /**
  345. </span> * Clears any auto refresh. See {@link #startAutoRefresh}.
  346. */
  347. stopAutoRefresh: function(){
  348. clearInterval(this.autoRefresh);
  349. delete this.autoRefresh;
  350. },
  351. <span id='Ext-ElementLoader-method-isAutoRefreshing'> /**
  352. </span> * Checks whether the loader is automatically refreshing. See {@link #startAutoRefresh}.
  353. * @return {Boolean} True if the loader is automatically refreshing
  354. */
  355. isAutoRefreshing: function(){
  356. return Ext.isDefined(this.autoRefresh);
  357. },
  358. <span id='Ext-ElementLoader-method-destroy'> /**
  359. </span> * Destroys the loader. Any active requests will be aborted.
  360. */
  361. destroy: function(){
  362. var me = this;
  363. me.stopAutoRefresh();
  364. delete me.target;
  365. me.abort();
  366. me.clearListeners();
  367. }
  368. });
  369. </pre>
  370. </body>
  371. </html>