SimManager.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. GNU General Public License Usage
  6. 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.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. /**
  10. * @author Don Griffin
  11. * @class Ext.ux.ajax.SimManager
  12. *
  13. * This singleton manages simulated Ajax responses. This allows application logic to be
  14. * written unaware that its Ajax calls are being handled by simulations ("simlets"). This
  15. * is currently done by hooking {@link Ext.data.Connection} methods, so all users of that
  16. * class (and {@link Ext.Ajax} since it is a derived class) qualify for simulation.
  17. *
  18. * The requires hooks are inserted when either the {@link #init} method is called or the
  19. * first {@link Simlet} is registered. For example:
  20. *
  21. * Ext.onReady(function () {
  22. * initAjaxSim();
  23. *
  24. * // normal stuff
  25. * });
  26. *
  27. * function initAjaxSim () {
  28. * Ext.ux.ajax.SimManager.init({
  29. * delay: 300
  30. * }).register({
  31. * '/app/data/url': {
  32. * stype: 'json', // use JsonSimlet (stype is like xtype for components)
  33. * data: [
  34. * { foo: 42, bar: 'abc' },
  35. * ...
  36. * ]
  37. * }
  38. * });
  39. * }
  40. *
  41. * As many URL's as desired can be registered and associated with a {@link Simlet}. To make
  42. * non-simulated Ajax requests once this singleton is initialized, add a `nosim:true` option
  43. * to the Ajax options:
  44. *
  45. * Ext.Ajax.request({
  46. * url: 'page.php',
  47. * nosim: true, // ignored by normal Ajax request
  48. * params: {
  49. * id: 1
  50. * },
  51. * success: function(response){
  52. * var text = response.responseText;
  53. * // process server response here
  54. * }
  55. * });
  56. *
  57. * @markdown
  58. */
  59. Ext.define('Ext.ux.ajax.SimManager', {
  60. singleton: true,
  61. requires: [
  62. 'Ext.data.Connection',
  63. 'Ext.ux.ajax.SimXhr',
  64. 'Ext.ux.ajax.Simlet',
  65. 'Ext.ux.ajax.JsonSimlet'
  66. ],
  67. /**
  68. * @cfg {Ext.ux.ajax.Simlet} defaultSimlet
  69. * The {@link Simlet} instance to use for non-matching URL's. By default, this will
  70. * return 404. Set this to null to use real Ajax calls for non-matching URL's.
  71. */
  72. /**
  73. * @cfg {String} defaultType
  74. * The default `stype` to apply to generic {@link Simlet} configuration objects. The
  75. * default is 'basic'.
  76. */
  77. defaultType: 'basic',
  78. /**
  79. * @cfg {Number} delay
  80. * The number of milliseconds to delay before delivering a response to an async request.
  81. */
  82. delay: 150,
  83. /**
  84. * @prop {Boolean} ready
  85. * True once this singleton has initialized and applied its Ajax hooks.
  86. * @private
  87. */
  88. ready: false,
  89. constructor: function () {
  90. this.simlets = {};
  91. },
  92. getXhr: function (url) {
  93. // Strip down to base URL (no query parameters or hash):
  94. var me = this,
  95. index = url.indexOf('?');
  96. if (index < 0) {
  97. index = url.indexOf('#');
  98. }
  99. if (index > 0) {
  100. url = url.substring(0, index);
  101. }
  102. var simlet = me.simlets[url] || me.defaultSimlet;
  103. if (simlet) {
  104. return Ext.create('Ext.ux.ajax.SimXhr', { mgr: me, simlet: simlet });
  105. }
  106. return null;
  107. },
  108. /**
  109. * Initializes this singleton and applies configuration options.
  110. * @param {Object} config An optional object with configuration properties to apply.
  111. * @return {SimManager} this
  112. * @markdown
  113. */
  114. init: function (config) {
  115. var me = this,
  116. proto = Ext.data.Connection.prototype,
  117. newXhr = proto.getXhrInstance,
  118. setOptions = proto.setOptions,
  119. url;
  120. Ext.apply(me, config);
  121. if (!me.ready) {
  122. me.ready = true;
  123. if (!('defaultSimlet' in me)) {
  124. me.defaultSimlet = Ext.create('Ext.ux.ajax.Simlet', {
  125. status: 404,
  126. statusText: 'Not Found'
  127. });
  128. }
  129. proto.getXhrInstance = function () {
  130. // the only way 'url' will be null is for nosim...
  131. var xhr = url && me.getXhr(url);
  132. if (!xhr) {
  133. xhr = newXhr.call(this);
  134. }
  135. return xhr;
  136. };
  137. proto.setOptions = function (options) {
  138. var ret = setOptions.apply(this, arguments);
  139. // remember the URL so we can give the right Simlet to the SimXhr...
  140. url = options.nosim ? null : ret.url;
  141. return ret;
  142. };
  143. }
  144. return me;
  145. },
  146. /**
  147. * Registeres one or more {@link Simlet} instances.
  148. * @param {Array/Object} simlet Either a {@link Simlet} instance or config, an Array
  149. * of such elements or an Object keyed by URL with values that are {@link Simlet}
  150. * instances or configs.
  151. * @markdown
  152. */
  153. register: function (simlet) {
  154. var me = this,
  155. i, n;
  156. me.init();
  157. function reg (one) {
  158. var simlet = one;
  159. if (!simlet.isSimlet) {
  160. simlet = Ext.create('simlet.' + (simlet.stype || me.defaultType), one);
  161. }
  162. me.simlets[one.url] = simlet;
  163. }
  164. if (Ext.isArray(simlet)) {
  165. Ext.each(simlet, reg);
  166. } else if (simlet.isSimlet || simlet.url) {
  167. reg(simlet);
  168. } else {
  169. Ext.Object.each(simlet, function (url, s) {
  170. s.url = url;
  171. reg(s);
  172. });
  173. }
  174. return me;
  175. }
  176. });