Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / docs / source / Rest.html
diff --git a/docs/source/Rest.html b/docs/source/Rest.html
new file mode 100644 (file)
index 0000000..8b1787a
--- /dev/null
@@ -0,0 +1,187 @@
+<!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-data.proxy.Rest'>/**
+</span> * @author Ed Spencer
+ * @class Ext.data.proxy.Rest
+ * @extends Ext.data.proxy.Ajax
+ * 
+ * &lt;p&gt;RestProxy is a specialization of the {@link Ext.data.proxy.Ajax AjaxProxy} which simply maps the four actions 
+ * (create, read, update and destroy) to RESTful HTTP verbs. For example, let's set up a {@link Ext.data.Model Model}
+ * with an inline RestProxy&lt;/p&gt;
+ * 
+&lt;pre&gt;&lt;code&gt;
+Ext.define('User', {
+    extend: 'Ext.data.Model',
+    fields: ['id', 'name', 'email'],
+
+    proxy: {
+        type: 'rest',
+        url : '/users'
+    }
+});
+&lt;/code&gt;&lt;/pre&gt;
+ * 
+ * &lt;p&gt;Now we can create a new User instance and save it via the RestProxy. Doing this will cause the Proxy to send a
+ * POST request to '/users':
+ * 
+&lt;pre&gt;&lt;code&gt;
+var user = Ext.ModelManager.create({name: 'Ed Spencer', email: 'ed@sencha.com'}, 'User');
+
+user.save(); //POST /users
+&lt;/code&gt;&lt;/pre&gt;
+ * 
+ * &lt;p&gt;Let's expand this a little and provide a callback for the {@link Ext.data.Model#save} call to update the Model
+ * once it has been created. We'll assume the creation went successfully and that the server gave this user an ID of 
+ * 123:&lt;/p&gt;
+ * 
+&lt;pre&gt;&lt;code&gt;
+user.save({
+    success: function(user) {
+        user.set('name', 'Khan Noonien Singh');
+
+        user.save(); //PUT /users/123
+    }
+});
+&lt;/code&gt;&lt;/pre&gt;
+ * 
+ * &lt;p&gt;Now that we're no longer creating a new Model instance, the request method is changed to an HTTP PUT, targeting
+ * the relevant url for that user. Now let's delete this user, which will use the DELETE method:&lt;/p&gt;
+ * 
+&lt;pre&gt;&lt;code&gt;
+    user.destroy(); //DELETE /users/123
+&lt;/code&gt;&lt;/pre&gt;
+ * 
+ * &lt;p&gt;Finally, when we perform a load of a Model or Store, RestProxy will use the GET method:&lt;/p&gt;
+ * 
+&lt;pre&gt;&lt;code&gt;
+//1. Load via Store
+
+//the Store automatically picks up the Proxy from the User model
+var store = new Ext.data.Store({
+    model: 'User'
+});
+
+store.load(); //GET /users
+
+//2. Load directly from the Model
+
+//GET /users/123
+Ext.ModelManager.getModel('User').load(123, {
+    success: function(user) {
+        console.log(user.getId()); //outputs 123
+    }
+});
+&lt;/code&gt;&lt;/pre&gt;
+ * 
+ * &lt;p&gt;&lt;u&gt;Url generation&lt;/u&gt;&lt;/p&gt;
+ * 
+ * &lt;p&gt;RestProxy is able to automatically generate the urls above based on two configuration options - {@link #appendId}
+ * and {@link #format}. If appendId is true (it is by default) then RestProxy will automatically append the ID of the 
+ * Model instance in question to the configured url, resulting in the '/users/123' that we saw above.&lt;/p&gt;
+ * 
+ * &lt;p&gt;If the request is not for a specific Model instance (e.g. loading a Store), the url is not appended with an id. 
+ * RestProxy will automatically insert a '/' before the ID if one is not already present.&lt;/p&gt;
+ * 
+&lt;pre&gt;&lt;code&gt;
+new Ext.data.proxy.Rest({
+    url: '/users',
+    appendId: true //default
+});
+
+// Collection url: /users
+// Instance url  : /users/123
+&lt;/code&gt;&lt;/pre&gt;
+ * 
+ * &lt;p&gt;RestProxy can also optionally append a format string to the end of any generated url:&lt;/p&gt;
+ * 
+&lt;pre&gt;&lt;code&gt;
+new Ext.data.proxy.Rest({
+    url: '/users',
+    format: 'json'
+});
+
+// Collection url: /users.json
+// Instance url  : /users/123.json
+&lt;/code&gt;&lt;/pre&gt;
+ * 
+ * &lt;p&gt;If further customization is needed, simply implement the {@link #buildUrl} method and add your custom generated
+ * url onto the {@link Ext.data.Request Request} object that is passed to buildUrl. See 
+ * &lt;a href=&quot;source/RestProxy.html#method-Ext.data.proxy.Rest-buildUrl&quot;&gt;RestProxy's implementation&lt;/a&gt; for an example of
+ * how to achieve this.&lt;/p&gt;
+ * 
+ * &lt;p&gt;Note that RestProxy inherits from {@link Ext.data.proxy.Ajax AjaxProxy}, which already injects all of the sorter,
+ * filter, group and paging options into the generated url. See the {@link Ext.data.proxy.Ajax AjaxProxy docs} for more
+ * details.&lt;/p&gt;
+ */
+Ext.define('Ext.data.proxy.Rest', {
+    extend: 'Ext.data.proxy.Ajax',
+    alternateClassName: 'Ext.data.RestProxy',
+    alias : 'proxy.rest',
+    
+<span id='Ext-data.proxy.Rest-cfg-appendId'>    /**
+</span>     * @cfg {Boolean} appendId True to automatically append the ID of a Model instance when performing a request based
+     * on that single instance. See RestProxy intro docs for more details. Defaults to true.
+     */
+    appendId: true,
+    
+<span id='Ext-data.proxy.Rest-cfg-format'>    /**
+</span>     * @cfg {String} format Optional data format to send to the server when making any request (e.g. 'json'). See the
+     * RestProxy intro docs for full details. Defaults to undefined.
+     */
+    
+<span id='Ext-data.proxy.Rest-cfg-batchActions'>    /**
+</span>     * @cfg {Boolean} batchActions True to batch actions of a particular type when synchronizing the store.
+     * Defaults to &lt;tt&gt;false&lt;/tt&gt;.
+     */
+    batchActions: false,
+    
+<span id='Ext-data.proxy.Rest-method-buildUrl'>    /**
+</span>     * Specialized version of buildUrl that incorporates the {@link #appendId} and {@link #format} options into the
+     * generated url. Override this to provide further customizations, but remember to call the superclass buildUrl
+     * so that additional parameters like the cache buster string are appended
+     */
+    buildUrl: function(request) {
+        var me        = this,
+            operation = request.operation,
+            records   = operation.records || [],
+            record    = records[0],
+            format    = me.format,
+            url       = me.getUrl(request),
+            id        = record ? record.getId() : operation.id;
+        
+        if (me.appendId &amp;&amp; id) {
+            if (!url.match(/\/$/)) {
+                url += '/';
+            }
+            
+            url += id;
+        }
+        
+        if (format) {
+            if (!url.match(/\.$/)) {
+                url += '.';
+            }
+            
+            url += format;
+        }
+        
+        request.url = url;
+        
+        return me.callParent(arguments);
+    }
+}, function() {
+    Ext.apply(this.prototype, {
+<span id='Ext-data.proxy.Rest-property-actionMethods'>        /**
+</span>         * Mapping of action name to HTTP request method. These default to RESTful conventions for the 'create', 'read',
+         * 'update' and 'destroy' actions (which map to 'POST', 'GET', 'PUT' and 'DELETE' respectively). This object should
+         * not be changed except globally via {@link Ext#override Ext.override} - the {@link #getMethod} function can be overridden instead.
+         * @property actionMethods
+         * @type Object
+         */
+        actionMethods: {
+            create : 'POST',
+            read   : 'GET',
+            update : 'PUT',
+            destroy: 'DELETE'
+        }
+    });
+});
+</pre></pre></body></html>
\ No newline at end of file