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