ComponentLoader.html 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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-ComponentLoader'>/**
  19. </span> * This class is used to load content via Ajax into a {@link Ext.Component}. In general
  20. * this class will not be instanced directly, rather a loader configuration will be passed to the
  21. * constructor of the {@link Ext.Component}.
  22. *
  23. * ## HTML Renderer
  24. *
  25. * By default, the content loaded will be processed as raw html. The response text
  26. * from the request is taken and added to the component. This can be used in
  27. * conjunction with the {@link #scripts} option to execute any inline scripts in
  28. * the resulting content. Using this renderer has the same effect as passing the
  29. * {@link Ext.Component#html} configuration option.
  30. *
  31. * ## Data Renderer
  32. *
  33. * This renderer allows content to be added by using JSON data and a {@link Ext.XTemplate}.
  34. * The content received from the response is passed to the {@link Ext.Component#update} method.
  35. * This content is run through the attached {@link Ext.Component#tpl} and the data is added to
  36. * the Component. Using this renderer has the same effect as using the {@link Ext.Component#data}
  37. * configuration in conjunction with a {@link Ext.Component#tpl}.
  38. *
  39. * ## Component Renderer
  40. *
  41. * This renderer can only be used with a {@link Ext.container.Container} and subclasses. It allows for
  42. * Components to be loaded remotely into a Container. The response is expected to be a single/series of
  43. * {@link Ext.Component} configuration objects. When the response is received, the data is decoded
  44. * and then passed to {@link Ext.container.Container#method-add}. Using this renderer has the same effect as specifying
  45. * the {@link Ext.container.Container#cfg-items} configuration on a Container.
  46. *
  47. * ## Custom Renderer
  48. *
  49. * A custom function can be passed to handle any other special case, see the {@link #renderer} option.
  50. *
  51. * ## Example Usage
  52. *
  53. * var cmp = Ext.create('Ext.Component', {
  54. * renderTo: Ext.getBody(),
  55. * tpl: '{firstName} - {lastName}',
  56. * loader: {
  57. * url: 'myPage.php',
  58. * renderer: 'data',
  59. * params: {
  60. * userId: 1
  61. * }
  62. * }
  63. * });
  64. *
  65. * // call the loader manually (or use autoLoad:true instead)
  66. * cmp.getLoader().load();
  67. */
  68. Ext.define('Ext.ComponentLoader', {
  69. /* Begin Definitions */
  70. extend: 'Ext.ElementLoader',
  71. statics: {
  72. Renderer: {
  73. Data: function(loader, response, active){
  74. var success = true;
  75. try {
  76. loader.getTarget().update(Ext.decode(response.responseText));
  77. } catch (e) {
  78. success = false;
  79. }
  80. return success;
  81. },
  82. Component: function(loader, response, active){
  83. var success = true,
  84. target = loader.getTarget(),
  85. items = [];
  86. //&lt;debug&gt;
  87. if (!target.isContainer) {
  88. Ext.Error.raise({
  89. target: target,
  90. msg: 'Components can only be loaded into a container'
  91. });
  92. }
  93. //&lt;/debug&gt;
  94. try {
  95. items = Ext.decode(response.responseText);
  96. } catch (e) {
  97. success = false;
  98. }
  99. if (success) {
  100. target.suspendLayouts();
  101. if (active.removeAll) {
  102. target.removeAll();
  103. }
  104. target.add(items);
  105. target.resumeLayouts(true);
  106. }
  107. return success;
  108. }
  109. }
  110. },
  111. /* End Definitions */
  112. <span id='Ext-ComponentLoader-cfg-target'> /**
  113. </span> * @cfg {Ext.Component/String} target The target {@link Ext.Component} for the loader.
  114. * If a string is passed it will be looked up via the id.
  115. */
  116. target: null,
  117. <span id='Ext-ComponentLoader-cfg-loadMask'> /**
  118. </span> * @cfg {Boolean/Object} loadMask True or a {@link Ext.LoadMask} configuration to enable masking during loading.
  119. */
  120. loadMask: false,
  121. <span id='Ext-ComponentLoader-cfg-scripts'> /**
  122. </span> * @cfg {Boolean} scripts True to parse any inline script tags in the response. This only used when using the html
  123. * {@link #renderer}.
  124. */
  125. <span id='Ext-ComponentLoader-cfg-renderer'> /**
  126. </span> * @cfg {String/Function} renderer
  127. The type of content that is to be loaded into, which can be one of 3 types:
  128. + **html** : Loads raw html content, see {@link Ext.Component#html}
  129. + **data** : Loads raw html content, see {@link Ext.Component#data}
  130. + **component** : Loads child {Ext.Component} instances. This option is only valid when used with a Container.
  131. Alternatively, you can pass a function which is called with the following parameters.
  132. + loader - Loader instance
  133. + response - The server response
  134. + active - The active request
  135. The function must return false is loading is not successful. Below is a sample of using a custom renderer:
  136. new Ext.Component({
  137. loader: {
  138. url: 'myPage.php',
  139. renderer: function(loader, response, active) {
  140. var text = response.responseText;
  141. loader.getTarget().update('The response is ' + text);
  142. return true;
  143. }
  144. }
  145. });
  146. */
  147. renderer: 'html',
  148. <span id='Ext-ComponentLoader-method-setTarget'> /**
  149. </span> * Set a {Ext.Component} as the target of this loader. Note that if the target is changed,
  150. * any active requests will be aborted.
  151. * @param {String/Ext.Component} target The component to be the target of this loader. If a string is passed
  152. * it will be looked up via its id.
  153. */
  154. setTarget: function(target){
  155. var me = this;
  156. if (Ext.isString(target)) {
  157. target = Ext.getCmp(target);
  158. }
  159. if (me.target &amp;&amp; me.target != target) {
  160. me.abort();
  161. }
  162. me.target = target;
  163. },
  164. // inherit docs
  165. removeMask: function(){
  166. this.target.setLoading(false);
  167. },
  168. <span id='Ext-ComponentLoader-method-addMask'> /**
  169. </span> * Add the mask on the target
  170. * @private
  171. * @param {Boolean/Object} mask The mask configuration
  172. */
  173. addMask: function(mask){
  174. this.target.setLoading(mask);
  175. },
  176. setOptions: function(active, options){
  177. active.removeAll = Ext.isDefined(options.removeAll) ? options.removeAll : this.removeAll;
  178. },
  179. <span id='Ext-ComponentLoader-method-getRenderer'> /**
  180. </span> * Gets the renderer to use
  181. * @private
  182. * @param {String/Function} renderer The renderer to use
  183. * @return {Function} A rendering function to use.
  184. */
  185. getRenderer: function(renderer){
  186. if (Ext.isFunction(renderer)) {
  187. return renderer;
  188. }
  189. var renderers = this.statics().Renderer;
  190. switch (renderer) {
  191. case 'component':
  192. return renderers.Component;
  193. case 'data':
  194. return renderers.Data;
  195. default:
  196. return Ext.ElementLoader.Renderer.Html;
  197. }
  198. }
  199. });
  200. </pre>
  201. </body>
  202. </html>