SimManager.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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-ux-ajax-SimManager'>/**
  19. </span> * @author Don Griffin
  20. *
  21. * This singleton manages simulated Ajax responses. This allows application logic to be
  22. * written unaware that its Ajax calls are being handled by simulations (&quot;simlets&quot;). This
  23. * is currently done by hooking {@link Ext.data.Connection} methods, so all users of that
  24. * class (and {@link Ext.Ajax} since it is a derived class) qualify for simulation.
  25. *
  26. * The requires hooks are inserted when either the {@link #init} method is called or the
  27. * first {@link Ext.ux.ajax.Simlet} is registered. For example:
  28. *
  29. * Ext.onReady(function () {
  30. * initAjaxSim();
  31. *
  32. * // normal stuff
  33. * });
  34. *
  35. * function initAjaxSim () {
  36. * Ext.ux.ajax.SimManager.init({
  37. * delay: 300
  38. * }).register({
  39. * '/app/data/url': {
  40. * stype: 'json', // use JsonSimlet (stype is like xtype for components)
  41. * data: [
  42. * { foo: 42, bar: 'abc' },
  43. * ...
  44. * ]
  45. * }
  46. * });
  47. * }
  48. *
  49. * As many URL's as desired can be registered and associated with a {@link Ext.ux.ajax.Simlet}. To make
  50. * non-simulated Ajax requests once this singleton is initialized, add a `nosim:true` option
  51. * to the Ajax options:
  52. *
  53. * Ext.Ajax.request({
  54. * url: 'page.php',
  55. * nosim: true, // ignored by normal Ajax request
  56. * params: {
  57. * id: 1
  58. * },
  59. * success: function(response){
  60. * var text = response.responseText;
  61. * // process server response here
  62. * }
  63. * });
  64. */
  65. Ext.define('Ext.ux.ajax.SimManager', {
  66. singleton: true,
  67. requires: [
  68. 'Ext.data.Connection',
  69. 'Ext.ux.ajax.SimXhr',
  70. 'Ext.ux.ajax.Simlet',
  71. 'Ext.ux.ajax.JsonSimlet'
  72. ],
  73. <span id='Ext-ux-ajax-SimManager-cfg-defaultSimlet'> /**
  74. </span> * @cfg {Ext.ux.ajax.Simlet} defaultSimlet
  75. * The {@link Ext.ux.ajax.Simlet} instance to use for non-matching URL's. By default, this will
  76. * return 404. Set this to null to use real Ajax calls for non-matching URL's.
  77. */
  78. <span id='Ext-ux-ajax-SimManager-cfg-defaultType'> /**
  79. </span> * @cfg {String} defaultType
  80. * The default `stype` to apply to generic {@link Ext.ux.ajax.Simlet} configuration objects. The
  81. * default is 'basic'.
  82. */
  83. defaultType: 'basic',
  84. <span id='Ext-ux-ajax-SimManager-cfg-delay'> /**
  85. </span> * @cfg {Number} delay
  86. * The number of milliseconds to delay before delivering a response to an async request.
  87. */
  88. delay: 150,
  89. <span id='Ext-ux-ajax-SimManager-property-ready'> /**
  90. </span> * @prop {Boolean} ready
  91. * True once this singleton has initialized and applied its Ajax hooks.
  92. * @private
  93. */
  94. ready: false,
  95. constructor: function () {
  96. this.simlets = {};
  97. },
  98. getSimlet: function (url) {
  99. // Strip down to base URL (no query parameters or hash):
  100. var me = this,
  101. index = url.indexOf('?');
  102. if (index &lt; 0) {
  103. index = url.indexOf('#');
  104. }
  105. if (index &gt; 0) {
  106. url = url.substring(0, index);
  107. }
  108. return me.simlets[url] || me.defaultSimlet;
  109. },
  110. getXhr: function (method, url, options, async) {
  111. var simlet = this.getSimlet(url);
  112. if (simlet) {
  113. return simlet.openRequest(method, url, options, async);
  114. }
  115. return null;
  116. },
  117. <span id='Ext-ux-ajax-SimManager-method-init'> /**
  118. </span> * Initializes this singleton and applies configuration options.
  119. * @param {Object} config An optional object with configuration properties to apply.
  120. * @return {Ext.ux.ajax.SimManager} this
  121. * @markdown
  122. */
  123. init: function (config) {
  124. var me = this;
  125. Ext.apply(me, config);
  126. if (!me.ready) {
  127. me.ready = true;
  128. if (!('defaultSimlet' in me)) {
  129. me.defaultSimlet = new Ext.ux.ajax.Simlet({
  130. status: 404,
  131. statusText: 'Not Found'
  132. });
  133. }
  134. me._openRequest = Ext.data.Connection.prototype.openRequest;
  135. Ext.data.Connection.override({
  136. openRequest: function (options, requestOptions, async) {
  137. var xhr = !options.nosim &amp;&amp;
  138. me.getXhr(requestOptions.method, requestOptions.url, options, async);
  139. if (!xhr) {
  140. xhr = this.callParent(arguments);
  141. }
  142. return xhr;
  143. }
  144. });
  145. if (Ext.data.JsonP) {
  146. Ext.data.JsonP.self.override({
  147. createScript: function (url, params, options) {
  148. var fullUrl = Ext.urlAppend(url, Ext.Object.toQueryString(params)),
  149. script = !options.nosim &amp;&amp;
  150. me.getXhr('GET', fullUrl, options, true);
  151. if (!script) {
  152. script = this.callParent(arguments);
  153. }
  154. return script;
  155. },
  156. loadScript: function (request) {
  157. var script = request.script;
  158. if (script.simlet) {
  159. script.jsonpCallback = request.params[request.callbackKey];
  160. script.send(null);
  161. } else {
  162. this.callParent(arguments);
  163. }
  164. }
  165. });
  166. }
  167. }
  168. return me;
  169. },
  170. openRequest: function (method, url, async) {
  171. var opt = {
  172. method: method,
  173. url: url
  174. };
  175. return this._openRequest.call(Ext.data.Connection.prototype, {}, opt, async);
  176. },
  177. <span id='Ext-ux-ajax-SimManager-method-register'> /**
  178. </span> * Registeres one or more {@link Ext.ux.ajax.Simlet} instances.
  179. * @param {Array/Object} simlet Either a {@link Ext.ux.ajax.Simlet} instance or config, an Array
  180. * of such elements or an Object keyed by URL with values that are {@link Ext.ux.ajax.Simlet}
  181. * instances or configs.
  182. * @markdown
  183. */
  184. register: function (simlet) {
  185. var me = this;
  186. me.init();
  187. function reg (one) {
  188. var simlet = one;
  189. if (!simlet.isSimlet) {
  190. simlet = Ext.create('simlet.' + (simlet.stype || me.defaultType), one);
  191. }
  192. me.simlets[one.url] = simlet;
  193. simlet.manager = me;
  194. }
  195. if (Ext.isArray(simlet)) {
  196. Ext.each(simlet, reg);
  197. } else if (simlet.isSimlet || simlet.url) {
  198. reg(simlet);
  199. } else {
  200. Ext.Object.each(simlet, function (url, s) {
  201. s.url = url;
  202. reg(s);
  203. });
  204. }
  205. return me;
  206. }
  207. });
  208. </pre>
  209. </body>
  210. </html>