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.SimManager
19 * This singleton manages simulated Ajax responses. This allows application logic to be
20 * written unaware that its Ajax calls are being handled by simulations ("simlets"). This
21 * is currently done by hooking {@link Ext.data.Connection} methods, so all users of that
22 * class (and {@link Ext.Ajax} since it is a derived class) qualify for simulation.
24 * The requires hooks are inserted when either the {@link #init} method is called or the
25 * first {@link Simlet} is registered. For example:
27 * Ext.onReady(function () {
33 * function initAjaxSim () {
34 * Ext.ux.ajax.SimManager.init({
38 * stype: 'json', // use JsonSimlet (stype is like xtype for components)
40 * { foo: 42, bar: 'abc' },
47 * As many URL's as desired can be registered and associated with a {@link Simlet}. To make
48 * non-simulated Ajax requests once this singleton is initialized, add a `nosim:true` option
49 * to the Ajax options:
53 * nosim: true, // ignored by normal Ajax request
57 * success: function(response){
58 * var text = response.responseText;
59 * // process server response here
65 Ext.define('Ext.ux.ajax.SimManager', {
69 'Ext.data.Connection',
72 'Ext.ux.ajax.JsonSimlet'
76 * @cfg {Ext.ux.ajax.Simlet} defaultSimlet
77 * The {@link Simlet} instance to use for non-matching URL's. By default, this will
78 * return 404. Set this to null to use real Ajax calls for non-matching URL's.
82 * @cfg {String} defaultType
83 * The default `stype` to apply to generic {@link Simlet} configuration objects. The
90 * The number of milliseconds to delay before delivering a response to an async request.
95 * @prop {Boolean} ready
96 * True once this singleton has initialized and applied its Ajax hooks.
101 constructor: function () {
105 getXhr: function (url) {
106 // Strip down to base URL (no query parameters or hash):
108 index = url.indexOf('?');
111 index = url.indexOf('#');
114 url = url.substring(0, index);
117 var simlet = me.simlets[url] || me.defaultSimlet;
120 return Ext.create('Ext.ux.ajax.SimXhr', { mgr: me, simlet: simlet });
126 * Initializes this singleton and applies configuration options.
127 * @param {Object} config An optional object with configuration properties to apply.
128 * @return {SimManager} this
131 init: function (config) {
133 proto = Ext.data.Connection.prototype,
134 newXhr = proto.getXhrInstance,
135 setOptions = proto.setOptions,
138 Ext.apply(me, config);
143 if (!('defaultSimlet' in me)) {
144 me.defaultSimlet = Ext.create('Ext.ux.ajax.Simlet', {
146 statusText: 'Not Found'
150 proto.getXhrInstance = function () {
151 // the only way 'url' will be null is for nosim...
152 var xhr = url && me.getXhr(url);
154 xhr = newXhr.call(this);
159 proto.setOptions = function (options) {
160 var ret = setOptions.apply(this, arguments);
161 // remember the URL so we can give the right Simlet to the SimXhr...
162 url = options.nosim ? null : ret.url;
171 * Registeres one or more {@link Simlet} instances.
172 * @param {Array/Object} simlet Either a {@link Simlet} instance or config, an Array
173 * of such elements or an Object keyed by URL with values that are {@link Simlet}
174 * instances or configs.
177 register: function (simlet) {
185 if (!simlet.isSimlet) {
186 simlet = Ext.create('simlet.' + (simlet.stype || me.defaultType), one);
188 me.simlets[one.url] = simlet;
191 if (Ext.isArray(simlet)) {
192 Ext.each(simlet, reg);
193 } else if (simlet.isSimlet || simlet.url) {
196 Ext.Object.each(simlet, function (url, s) {