/ext-4.1.0_b3/docs/source/Simlet.html

https://bitbucket.org/srogerf/javascript · HTML · 201 lines · 170 code · 31 blank · 0 comment · 0 complexity · 7a8a47f55585b415631278522771e210 MD5 · raw file

  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-Simlet'>/**
  19. </span> * @author Don Griffin
  20. *
  21. * This is a base class for more advanced &quot;simlets&quot; (simulated servers). A simlet is asked
  22. * to provide a response given a {@link Ext.ux.ajax.SimXhr} instance.
  23. */
  24. Ext.define('Ext.ux.ajax.Simlet', function () {
  25. var urlRegex = /([^?#]*)(#.*)?$/,
  26. dateRegex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/,
  27. intRegex = /^[+-]?\d+$/,
  28. floatRegex = /^[+-]?\d+\.\d+$/;
  29. function parseParamValue (value) {
  30. var m;
  31. if (Ext.isDefined(value)) {
  32. value = decodeURIComponent(value);
  33. if (intRegex.test(value)) {
  34. value = parseInt(value, 10);
  35. } else if (floatRegex.test(value)) {
  36. value = parseFloat(value);
  37. } else if (!!(m = dateRegex.test(value))) {
  38. value = new Date(Date.UTC(+m[1], +m[2]-1, +m[3], +m[4], +m[5], +m[6]));
  39. }
  40. }
  41. return value;
  42. }
  43. return {
  44. alias: 'simlet.basic',
  45. isSimlet: true,
  46. responseProps: ['responseText', 'responseXML', 'status', 'statusText'],
  47. <span id='Ext-ux-ajax-Simlet-cfg-responseText'> /**
  48. </span> * @cfg {Number} responseText
  49. */
  50. <span id='Ext-ux-ajax-Simlet-cfg-responseXML'> /**
  51. </span> * @cfg {Number} responseXML
  52. */
  53. <span id='Ext-ux-ajax-Simlet-cfg-responseHeaders'> /**
  54. </span> * @cfg {Object} responseHeaders
  55. */
  56. <span id='Ext-ux-ajax-Simlet-cfg-status'> /**
  57. </span> * @cfg {Number} status
  58. */
  59. status: 200,
  60. <span id='Ext-ux-ajax-Simlet-cfg-statusText'> /**
  61. </span> * @cfg {String} statusText
  62. */
  63. statusText: 'OK',
  64. constructor: function (config) {
  65. Ext.apply(this, config);
  66. },
  67. doGet: function (ctx) {
  68. var me = this,
  69. ret = {};
  70. Ext.each(me.responseProps, function (prop) {
  71. if (prop in me) {
  72. ret[prop] = me[prop];
  73. }
  74. });
  75. return ret;
  76. },
  77. doRedirect: function (ctx) {
  78. return false;
  79. },
  80. <span id='Ext-ux-ajax-Simlet-method-exec'> /**
  81. </span> * Performs the action requested by the given XHR and returns an object to be applied
  82. * on to the XHR (containing `status`, `responseText`, etc.). For the most part,
  83. * this is delegated to `doMethod` methods on this class, such as `doGet`.
  84. *
  85. * @param {Ext.ux.ajax.SimXhr} xhr The simulated XMLHttpRequest instance.
  86. * @returns {Object} The response properties to add to the XMLHttpRequest.
  87. */
  88. exec: function (xhr) {
  89. var me = this,
  90. ret = {},
  91. method = 'do' + Ext.String.capitalize(xhr.method.toLowerCase()), // doGet
  92. fn = me[method];
  93. if (fn) {
  94. ret = fn.call(me, me.getCtx(xhr.method, xhr.url, xhr));
  95. } else {
  96. ret = { status: 405, statusText: 'Method Not Allowed' };
  97. }
  98. return ret;
  99. },
  100. getCtx: function (method, url, xhr) {
  101. return {
  102. method: method,
  103. params: this.parseQueryString(url),
  104. url: url,
  105. xhr: xhr
  106. };
  107. },
  108. openRequest: function (method, url, options, async) {
  109. var ctx = this.getCtx(method, url),
  110. redirect = this.doRedirect(ctx),
  111. xhr;
  112. if (redirect) {
  113. xhr = redirect;
  114. } else {
  115. xhr = new Ext.ux.ajax.SimXhr({
  116. mgr: this.manager,
  117. simlet: this,
  118. options: options
  119. });
  120. xhr.open(method, url, async);
  121. }
  122. return xhr;
  123. },
  124. parseQueryString : function (str) {
  125. var m = urlRegex.exec(str),
  126. ret = {},
  127. key,
  128. value,
  129. i, n;
  130. if (m &amp;&amp; m[1]) {
  131. var pair, parts = m[1].split('&amp;');
  132. for (i = 0, n = parts.length; i &lt; n; ++i) {
  133. if ((pair = parts[i].split('='))[0]) {
  134. key = decodeURIComponent(pair.shift());
  135. value = parseParamValue((pair.length &gt; 1) ? pair.join('=') : pair[0]);
  136. if (!(key in ret)) {
  137. ret[key] = value;
  138. } else if (Ext.isArray(ret[key])) {
  139. ret[key].push(value);
  140. } else {
  141. ret[key] = [ret[key], value];
  142. }
  143. }
  144. }
  145. }
  146. return ret;
  147. },
  148. redirect: function (method, url, params) {
  149. switch (arguments.length) {
  150. case 2:
  151. if (typeof url == 'string') {
  152. break;
  153. }
  154. params = url;
  155. // fall...
  156. case 1:
  157. url = method;
  158. method = 'GET';
  159. break;
  160. }
  161. if (params) {
  162. url = Ext.urlAppend(url, Ext.Object.toQueryString(params));
  163. }
  164. return this.manager.openRequest(method, url);
  165. }
  166. };
  167. }());
  168. </pre>
  169. </body>
  170. </html>