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; }
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
21 * The Rest proxy is a specialization of the {@link Ext.data.proxy.Ajax AjaxProxy} which simply maps the four actions
22 * (create, read, update and destroy) to RESTful HTTP verbs. For example, let's set up a {@link Ext.data.Model Model}
23 * with an inline Rest proxy
25 * Ext.define('User', {
26 * extend: 'Ext.data.Model',
27 * fields: ['id', 'name', 'email'],
35 * Now we can create a new User instance and save it via the Rest proxy. Doing this will cause the Proxy to send a POST
36 * request to '/users':
38 * var user = Ext.create('User', {name: 'Ed Spencer', email: 'ed@sencha.com'});
40 * user.save(); //POST /users
42 * Let's expand this a little and provide a callback for the {@link Ext.data.Model#save} call to update the Model once
43 * it has been created. We'll assume the creation went successfully and that the server gave this user an ID of 123:
46 * success: function(user) {
47 * user.set('name', 'Khan Noonien Singh');
49 * user.save(); //PUT /users/123
53 * Now that we're no longer creating a new Model instance, the request method is changed to an HTTP PUT, targeting the
54 * relevant url for that user. Now let's delete this user, which will use the DELETE method:
56 * user.destroy(); //DELETE /users/123
58 * Finally, when we perform a load of a Model or Store, Rest proxy will use the GET method:
62 * //the Store automatically picks up the Proxy from the User model
63 * var store = Ext.create('Ext.data.Store', {
67 * store.load(); //GET /users
69 * //2. Load directly from the Model
72 * Ext.ModelManager.getModel('User').load(123, {
73 * success: function(user) {
74 * console.log(user.getId()); //outputs 123
80 * The Rest proxy is able to automatically generate the urls above based on two configuration options - {@link #appendId} and
81 * {@link #format}. If appendId is true (it is by default) then Rest proxy will automatically append the ID of the Model
82 * instance in question to the configured url, resulting in the '/users/123' that we saw above.
84 * If the request is not for a specific Model instance (e.g. loading a Store), the url is not appended with an id.
85 * The Rest proxy will automatically insert a '/' before the ID if one is not already present.
87 * new Ext.data.proxy.Rest({
89 * appendId: true //default
92 * // Collection url: /users
93 * // Instance url : /users/123
95 * The Rest proxy can also optionally append a format string to the end of any generated url:
97 * new Ext.data.proxy.Rest({
102 * // Collection url: /users.json
103 * // Instance url : /users/123.json
105 * If further customization is needed, simply implement the {@link #buildUrl} method and add your custom generated url
106 * onto the {@link Ext.data.Request Request} object that is passed to buildUrl. See [Rest proxy's implementation][1] for
107 * an example of how to achieve this.
109 * Note that Rest proxy inherits from {@link Ext.data.proxy.Ajax AjaxProxy}, which already injects all of the sorter,
110 * filter, group and paging options into the generated url. See the {@link Ext.data.proxy.Ajax AjaxProxy docs} for more
113 * [1]: source/RestProxy.html#method-Ext.data.proxy.Rest-buildUrl
115 Ext.define('Ext.data.proxy.Rest', {
116 extend: 'Ext.data.proxy.Ajax',
117 alternateClassName: 'Ext.data.RestProxy',
118 alias : 'proxy.rest',
120 <span id='Ext-data-proxy-Rest-cfg-appendId'> /**
121 </span> * @cfg {Boolean} appendId
122 * True to automatically append the ID of a Model instance when performing a request based on that single instance.
123 * See Rest proxy intro docs for more details. Defaults to true.
127 <span id='Ext-data-proxy-Rest-cfg-format'> /**
128 </span> * @cfg {String} format
129 * Optional data format to send to the server when making any request (e.g. 'json'). See the Rest proxy intro docs
130 * for full details. Defaults to undefined.
133 <span id='Ext-data-proxy-Rest-cfg-batchActions'> /**
134 </span> * @cfg {Boolean} batchActions
135 * True to batch actions of a particular type when synchronizing the store. Defaults to false.
139 <span id='Ext-data-proxy-Rest-method-buildUrl'> /**
140 </span> * Specialized version of buildUrl that incorporates the {@link #appendId} and {@link #format} options into the
141 * generated url. Override this to provide further customizations, but remember to call the superclass buildUrl so
142 * that additional parameters like the cache buster string are appended.
143 * @param {Object} request
145 buildUrl: function(request) {
147 operation = request.operation,
148 records = operation.records || [],
151 url = me.getUrl(request),
152 id = record ? record.getId() : operation.id;
154 if (me.appendId && id) {
155 if (!url.match(/\/$/)) {
163 if (!url.match(/\.$/)) {
172 return me.callParent(arguments);
175 Ext.apply(this.prototype, {
176 <span id='Ext-data-proxy-Rest-property-actionMethods'> /**
177 </span> * @property {Object} actionMethods
178 * Mapping of action name to HTTP request method. These default to RESTful conventions for the 'create', 'read',
179 * 'update' and 'destroy' actions (which map to 'POST', 'GET', 'PUT' and 'DELETE' respectively). This object
180 * should not be changed except globally via {@link Ext#override Ext.override} - the {@link #getMethod} function
181 * can be overridden instead.