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-Rest'>/**
19 </span> * @author Ed Spencer
20 * @class Ext.data.proxy.Rest
21 * @extends Ext.data.proxy.Ajax
23 * <p>RestProxy is a specialization of the {@link Ext.data.proxy.Ajax AjaxProxy} which simply maps the four actions
24 * (create, read, update and destroy) to RESTful HTTP verbs. For example, let's set up a {@link Ext.data.Model Model}
25 * with an inline RestProxy</p>
27 <pre><code>
29 extend: 'Ext.data.Model',
30 fields: ['id', 'name', 'email'],
37 </code></pre>
39 * <p>Now we can create a new User instance and save it via the RestProxy. Doing this will cause the Proxy to send a
40 * POST request to '/users':
42 <pre><code>
43 var user = Ext.ModelManager.create({name: 'Ed Spencer', email: 'ed@sencha.com'}, 'User');
45 user.save(); //POST /users
46 </code></pre>
48 * <p>Let's expand this a little and provide a callback for the {@link Ext.data.Model#save} call to update the Model
49 * once it has been created. We'll assume the creation went successfully and that the server gave this user an ID of
52 <pre><code>
54 success: function(user) {
55 user.set('name', 'Khan Noonien Singh');
57 user.save(); //PUT /users/123
60 </code></pre>
62 * <p>Now that we're no longer creating a new Model instance, the request method is changed to an HTTP PUT, targeting
63 * the relevant url for that user. Now let's delete this user, which will use the DELETE method:</p>
65 <pre><code>
66 user.destroy(); //DELETE /users/123
67 </code></pre>
69 * <p>Finally, when we perform a load of a Model or Store, RestProxy will use the GET method:</p>
71 <pre><code>
74 //the Store automatically picks up the Proxy from the User model
75 var store = new Ext.data.Store({
79 store.load(); //GET /users
81 //2. Load directly from the Model
84 Ext.ModelManager.getModel('User').load(123, {
85 success: function(user) {
86 console.log(user.getId()); //outputs 123
89 </code></pre>
91 * <p><u>Url generation</u></p>
93 * <p>RestProxy is able to automatically generate the urls above based on two configuration options - {@link #appendId}
94 * and {@link #format}. If appendId is true (it is by default) then RestProxy will automatically append the ID of the
95 * Model instance in question to the configured url, resulting in the '/users/123' that we saw above.</p>
97 * <p>If the request is not for a specific Model instance (e.g. loading a Store), the url is not appended with an id.
98 * RestProxy will automatically insert a '/' before the ID if one is not already present.</p>
100 <pre><code>
101 new Ext.data.proxy.Rest({
103 appendId: true //default
106 // Collection url: /users
107 // Instance url : /users/123
108 </code></pre>
110 * <p>RestProxy can also optionally append a format string to the end of any generated url:</p>
112 <pre><code>
113 new Ext.data.proxy.Rest({
118 // Collection url: /users.json
119 // Instance url : /users/123.json
120 </code></pre>
122 * <p>If further customization is needed, simply implement the {@link #buildUrl} method and add your custom generated
123 * url onto the {@link Ext.data.Request Request} object that is passed to buildUrl. See
124 * <a href="source/RestProxy.html#method-Ext.data.proxy.Rest-buildUrl">RestProxy's implementation</a> for an example of
125 * how to achieve this.</p>
127 * <p>Note that RestProxy inherits from {@link Ext.data.proxy.Ajax AjaxProxy}, which already injects all of the sorter,
128 * filter, group and paging options into the generated url. See the {@link Ext.data.proxy.Ajax AjaxProxy docs} for more
131 Ext.define('Ext.data.proxy.Rest', {
132 extend: 'Ext.data.proxy.Ajax',
133 alternateClassName: 'Ext.data.RestProxy',
134 alias : 'proxy.rest',
136 <span id='Ext-data-proxy-Rest-cfg-appendId'> /**
137 </span> * @cfg {Boolean} appendId True to automatically append the ID of a Model instance when performing a request based
138 * on that single instance. See RestProxy intro docs for more details. Defaults to true.
142 <span id='Ext-data-proxy-Rest-cfg-format'> /**
143 </span> * @cfg {String} format Optional data format to send to the server when making any request (e.g. 'json'). See the
144 * RestProxy intro docs for full details. Defaults to undefined.
147 <span id='Ext-data-proxy-Rest-cfg-batchActions'> /**
148 </span> * @cfg {Boolean} batchActions True to batch actions of a particular type when synchronizing the store.
149 * Defaults to <tt>false</tt>.
153 <span id='Ext-data-proxy-Rest-method-buildUrl'> /**
154 </span> * Specialized version of buildUrl that incorporates the {@link #appendId} and {@link #format} options into the
155 * generated url. Override this to provide further customizations, but remember to call the superclass buildUrl
156 * so that additional parameters like the cache buster string are appended
158 buildUrl: function(request) {
160 operation = request.operation,
161 records = operation.records || [],
164 url = me.getUrl(request),
165 id = record ? record.getId() : operation.id;
167 if (me.appendId && id) {
168 if (!url.match(/\/$/)) {
176 if (!url.match(/\.$/)) {
185 return me.callParent(arguments);
188 Ext.apply(this.prototype, {
189 <span id='Ext-data-proxy-Rest-property-actionMethods'> /**
190 </span> * Mapping of action name to HTTP request method. These default to RESTful conventions for the 'create', 'read',
191 * 'update' and 'destroy' actions (which map to 'POST', 'GET', 'PUT' and 'DELETE' respectively). This object should
192 * not be changed except globally via {@link Ext#override Ext.override} - the {@link #getMethod} function can be overridden instead.
193 * @property actionMethods