3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 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.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
17 * @class Ext.ux.ajax.Simlet
19 * This is a base class for more advanced "simlets" (simulated servers). A simlet is asked
20 * to provide a response given a {@link SimXhr} instance.
22 Ext.define('Ext.ux.ajax.Simlet', function () {
23 var urlRegex = /([^?#]*)(#.*)?$/,
24 dateRegex = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/,
25 intRegex = /^[+-]?\d+$/,
26 floatRegex = /^[+-]?\d+\.\d+$/;
28 function parseParamValue (value) {
31 if (Ext.isDefined(value)) {
32 value = decodeURIComponent(value);
34 if (intRegex.test(value)) {
35 value = parseInt(value, 10);
36 } else if (floatRegex.test(value)) {
37 value = parseFloat(value);
38 } else if (!!(m = dateRegex.test(value))) {
39 value = new Date(Date.UTC(+m[1], +m[2]-1, +m[3], +m[4], +m[5], +m[6]));
47 alias: 'simlet.basic',
51 responseProps: ['responseText', 'responseXML', 'status', 'statusText'],
54 * @cfg {Number} responseText
58 * @cfg {Number} responseXML
62 * @cfg {Object} responseHeaders
66 * @cfg {Number} status
71 * @cfg {String} statusText
75 constructor: function (config) {
76 Ext.apply(this, config);
79 doGet: function (ctx) {
83 Ext.each(me.responseProps, function (prop) {
93 * Performs the action requested by the given XHR and returns an object to be applied
94 * on to the XHR (containing `status`, `responseText`, etc.). For the most part,
95 * this is delegated to `doMethod` methods on this class, such as `doGet`.
97 * @param {SimXhr} xhr The simulated XMLHttpRequest instance.
98 * @returns {Object} The response properties to add to the XMLHttpRequest.
101 exec: function (xhr) {
104 method = 'do' + Ext.String.capitalize(xhr.method.toLowerCase()), // doGet
110 params: me.parseQueryString(xhr.url)
113 ret = { status: 405, statusText: 'Method Not Allowed' };
119 parseQueryString : function (str) {
120 var m = urlRegex.exec(str),
127 var pair, parts = m[1].split('&');
129 for (i = 0, n = parts.length; i < n; ++i) {
130 if ((pair = parts[i].split('='))[0]) {
131 key = decodeURIComponent(pair.shift());
132 value = parseParamValue((pair.length > 1) ? pair.join('=') : pair[0]);
136 } else if (Ext.isArray(ret[key])) {
137 ret[key].push(value);
139 ret[key] = [ret[key], value];