4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5 <title>The source code</title>
6 <link href="../prettify/prettify.css" type="text/css" rel="stylesheet" />
7 <script type="text/javascript" src="../prettify/prettify.js"></script>
8 <style type="text/css">
9 .highlight { display: block; background-color: #ddd; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-data-proxy-Ajax-method-constructor'><span id='Ext-data-proxy-Ajax'>/**
19 </span></span> * @author Ed Spencer
20 * @class Ext.data.proxy.Ajax
21 * @extends Ext.data.proxy.Server
23 * <p>AjaxProxy is one of the most widely-used ways of getting data into your application. It uses AJAX requests to
24 * load data from the server, usually to be placed into a {@link Ext.data.Store Store}. Let's take a look at a typical
25 * setup. Here we're going to set up a Store that has an AjaxProxy. To prepare, we'll also set up a
26 * {@link Ext.data.Model Model}:</p>
28 <pre><code>
30 extend: 'Ext.data.Model',
31 fields: ['id', 'name', 'email']
34 //The Store contains the AjaxProxy as an inline configuration
35 var store = new Ext.data.Store({
44 </code></pre>
46 * <p>Our example is going to load user data into a Store, so we start off by defining a {@link Ext.data.Model Model}
47 * with the fields that we expect the server to return. Next we set up the Store itself, along with a {@link #proxy}
48 * configuration. This configuration was automatically turned into an Ext.data.proxy.Ajax instance, with the url we
49 * specified being passed into AjaxProxy's constructor. It's as if we'd done this:</p>
51 <pre><code>
52 new Ext.data.proxy.Ajax({
57 </code></pre>
59 * <p>A couple of extra configurations appeared here - {@link #model} and {@link #reader}. These are set by default
60 * when we create the proxy via the Store - the Store already knows about the Model, and Proxy's default
61 * {@link Ext.data.reader.Reader Reader} is {@link Ext.data.reader.Json JsonReader}.</p>
63 * <p>Now when we call store.load(), the AjaxProxy springs into action, making a request to the url we configured
64 * ('users.json' in this case). As we're performing a read, it sends a GET request to that url (see {@link #actionMethods}
65 * to customize this - by default any kind of read will be sent as a GET request and any kind of write will be sent as a
66 * POST request).</p>
68 * <p><u>Limitations</u></p>
70 * <p>AjaxProxy cannot be used to retrieve data from other domains. If your application is running on http://domainA.com
71 * it cannot load data from http://domainB.com because browsers have a built-in security policy that prohibits domains
72 * talking to each other via AJAX.</p>
74 * <p>If you need to read data from another domain and can't set up a proxy server (some software that runs on your own
75 * domain's web server and transparently forwards requests to http://domainB.com, making it look like they actually came
76 * from http://domainA.com), you can use {@link Ext.data.proxy.JsonP} and a technique known as JSON-P (JSON with
77 * Padding), which can help you get around the problem so long as the server on http://domainB.com is set up to support
78 * JSON-P responses. See {@link Ext.data.proxy.JsonP JsonPProxy}'s introduction docs for more details.</p>
80 * <p><u>Readers and Writers</u></p>
82 * <p>AjaxProxy can be configured to use any type of {@link Ext.data.reader.Reader Reader} to decode the server's response. If
83 * no Reader is supplied, AjaxProxy will default to using a {@link Ext.data.reader.Json JsonReader}. Reader configuration
84 * can be passed in as a simple object, which the Proxy automatically turns into a {@link Ext.data.reader.Reader Reader}
87 <pre><code>
88 var proxy = new Ext.data.proxy.Ajax({
96 proxy.getReader(); //returns an {@link Ext.data.reader.Xml XmlReader} instance based on the config we supplied
97 </code></pre>
99 * <p><u>Url generation</u></p>
101 * <p>AjaxProxy automatically inserts any sorting, filtering, paging and grouping options into the url it generates for
102 * each request. These are controlled with the following configuration options:</p>
104 * <ul style="list-style-type: disc; padding-left: 20px;">
105 * <li>{@link #pageParam} - controls how the page number is sent to the server
106 * (see also {@link #startParam} and {@link #limitParam})</li>
107 * <li>{@link #sortParam} - controls how sort information is sent to the server</li>
108 * <li>{@link #groupParam} - controls how grouping information is sent to the server</li>
109 * <li>{@link #filterParam} - controls how filter information is sent to the server</li>
112 * <p>Each request sent by AjaxProxy is described by an {@link Ext.data.Operation Operation}. To see how we can
113 * customize the generated urls, let's say we're loading the Proxy with the following Operation:</p>
115 <pre><code>
116 var operation = new Ext.data.Operation({
120 </code></pre>
122 * <p>Now we'll issue the request for this Operation by calling {@link #read}:</p>
124 <pre><code>
125 var proxy = new Ext.data.proxy.Ajax({
129 proxy.read(operation); //GET /users?page=2
130 </code></pre>
132 * <p>Easy enough - the Proxy just copied the page property from the Operation. We can customize how this page data is
133 * sent to the server:</p>
135 <pre><code>
136 var proxy = new Ext.data.proxy.Ajax({
138 pagePage: 'pageNumber'
141 proxy.read(operation); //GET /users?pageNumber=2
142 </code></pre>
144 * <p>Alternatively, our Operation could have been configured to send start and limit parameters instead of page:</p>
146 <pre><code>
147 var operation = new Ext.data.Operation({
153 var proxy = new Ext.data.proxy.Ajax({
157 proxy.read(operation); //GET /users?start=50&limit=25
158 </code></pre>
160 * <p>Again we can customize this url:</p>
162 <pre><code>
163 var proxy = new Ext.data.proxy.Ajax({
165 startParam: 'startIndex',
166 limitParam: 'limitIndex'
169 proxy.read(operation); //GET /users?startIndex=50&limitIndex=25
170 </code></pre>
172 * <p>AjaxProxy will also send sort and filter information to the server. Let's take a look at how this looks with a
173 * more expressive Operation object:</p>
175 <pre><code>
176 var operation = new Ext.data.Operation({
179 new Ext.util.Sorter({
183 new Ext.util.Sorter({
189 new Ext.util.Filter({
190 property: 'eyeColor',
195 </code></pre>
197 * <p>This is the type of object that is generated internally when loading a {@link Ext.data.Store Store} with sorters
198 * and filters defined. By default the AjaxProxy will JSON encode the sorters and filters, resulting in something like
199 * this (note that the url is escaped before sending the request, but is left unescaped here for clarity):</p>
201 <pre><code>
202 var proxy = new Ext.data.proxy.Ajax({
206 proxy.read(operation); //GET /users?sort=[{"property":"name","direction":"ASC"},{"property":"age","direction":"DESC"}]&filter=[{"property":"eyeColor","value":"brown"}]
207 </code></pre>
209 * <p>We can again customize how this is created by supplying a few configuration options. Let's say our server is set
210 * up to receive sorting information is a format like "sortBy=name#ASC,age#DESC". We can configure AjaxProxy to provide
211 * that format like this:</p>
213 <pre><code>
214 var proxy = new Ext.data.proxy.Ajax({
217 filterParam: 'filterBy',
219 //our custom implementation of sorter encoding - turns our sorters into "name#ASC,age#DESC"
220 encodeSorters: function(sorters) {
221 var length = sorters.length,
225 for (i = 0; i < length; i++) {
228 sortStrs[i] = sorter.property + '#' + sorter.direction
231 return sortStrs.join(",");
235 proxy.read(operation); //GET /users?sortBy=name#ASC,age#DESC&filterBy=[{"property":"eyeColor","value":"brown"}]
236 </code></pre>
238 * <p>We can also provide a custom {@link #encodeFilters} function to encode our filters.</p>
242 * <p>Note that if this HttpProxy is being used by a {@link Ext.data.Store Store}, then the
243 * Store's call to {@link #load} will override any specified <tt>callback</tt> and <tt>params</tt>
244 * options. In this case, use the Store's {@link Ext.data.Store#events events} to modify parameters,
245 * or react to loading events. The Store's {@link Ext.data.Store#baseParams baseParams} may also be
246 * used to pass parameters known at instantiation time.</p>
248 * <p>If an options parameter is passed, the singleton {@link Ext.Ajax} object will be used to make
249 * the request.</p>
251 Ext.define('Ext.data.proxy.Ajax', {
252 requires: ['Ext.util.MixedCollection', 'Ext.Ajax'],
253 extend: 'Ext.data.proxy.Server',
255 alternateClassName: ['Ext.data.HttpProxy', 'Ext.data.AjaxProxy'],
257 <span id='Ext-data-proxy-Ajax-property-actionMethods'> /**
258 </span> * @property actionMethods
259 * Mapping of action name to HTTP request method. In the basic AjaxProxy these are set to 'GET' for 'read' actions and 'POST'
260 * for 'create', 'update' and 'destroy' actions. The {@link Ext.data.proxy.Rest} maps these to the correct RESTful methods.
269 <span id='Ext-data-proxy-Ajax-cfg-headers'> /**
270 </span> * @cfg {Object} headers Any headers to add to the Ajax request. Defaults to <tt>undefined</tt>.
273 <span id='Ext-data-proxy-Ajax-method-doRequest'> /**
276 doRequest: function(operation, callback, scope) {
277 var writer = this.getWriter(),
278 request = this.buildRequest(operation, callback, scope);
280 if (operation.allowWrite()) {
281 request = writer.write(request);
285 headers : this.headers,
286 timeout : this.timeout,
288 callback : this.createRequestCallback(request, operation, callback, scope),
289 method : this.getMethod(request),
290 disableCaching: false // explicitly set it to false, ServerProxy handles caching
293 Ext.Ajax.request(request);
298 <span id='Ext-data-proxy-Ajax-method-getMethod'> /**
299 </span> * Returns the HTTP method name for a given request. By default this returns based on a lookup on {@link #actionMethods}.
300 * @param {Ext.data.Request} request The request object
301 * @return {String} The HTTP method to use (should be one of 'GET', 'POST', 'PUT' or 'DELETE')
303 getMethod: function(request) {
304 return this.actionMethods[request.action];
307 <span id='Ext-data-proxy-Ajax-method-createRequestCallback'> /**
309 * TODO: This is currently identical to the JsonPProxy version except for the return function's signature. There is a lot
310 * of code duplication inside the returned function so we need to find a way to DRY this up.
311 * @param {Ext.data.Request} request The Request object
312 * @param {Ext.data.Operation} operation The Operation being executed
313 * @param {Function} callback The callback function to be called when the request completes. This is usually the callback
314 * passed to doRequest
315 * @param {Object} scope The scope in which to execute the callback function
316 * @return {Function} The callback function
318 createRequestCallback: function(request, operation, callback, scope) {
321 return function(options, success, response) {
322 me.processResponse(success, operation, request, response, callback, scope);
326 //backwards compatibility, remove in Ext JS 5.0
327 Ext.data.HttpProxy = this;