Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / data / DataProxy.js
index 2f40235..11d2c62 100644 (file)
@@ -1,5 +1,5 @@
 /*!
- * Ext JS Library 3.0.0
+ * Ext JS Library 3.0.3
  * Copyright(c) 2006-2009 Ext JS, LLC
  * licensing@extjs.com
  * http://www.extjs.com/license
@@ -67,7 +67,27 @@ api: {
     update  : undefined,\r
     destroy : undefined\r
 }\r
-</code></pre>\r
+     * </code></pre>\r
+     * <p>The url is built based upon the action being executed <tt>[load|create|save|destroy]</tt>\r
+     * using the commensurate <tt>{@link #api}</tt> property, or if undefined default to the\r
+     * configured {@link Ext.data.Store}.{@link Ext.data.Store#url url}.</p><br>\r
+     * <p>For example:</p>\r
+     * <pre><code>\r
+api: {\r
+    load :    '/controller/load',\r
+    create :  '/controller/new',  // Server MUST return idProperty of new record\r
+    save :    '/controller/update',\r
+    destroy : '/controller/destroy_action'\r
+}\r
+\r
+// Alternatively, one can use the object-form to specify each API-action\r
+api: {\r
+    load: {url: 'read.php', method: 'GET'},\r
+    create: 'create.php',\r
+    destroy: 'destroy.php',\r
+    save: 'update.php'\r
+}\r
+     * </code></pre>\r
      * <p>If the specific URL for a given CRUD action is undefined, the CRUD action request\r
      * will be directed to the configured <tt>{@link Ext.data.Connection#url url}</tt>.</p>\r
      * <br><p><b>Note</b>: To modify the URL for an action dynamically the appropriate API\r
@@ -85,22 +105,9 @@ myStore.on({
             // permanent, applying this URL for all subsequent requests.\r
             store.proxy.setUrl('changed1.php', true);\r
 \r
-            // manually set the <b>private</b> connection URL.\r
-            // <b>Warning:</b>  Accessing the private URL property should be avoided.\r
-            // Use the public method <tt>{@link Ext.data.HttpProxy#setUrl setUrl}</tt> instead, shown above.\r
-            // It should be noted that changing the URL like this will affect\r
-            // the URL for just this request.  Subsequent requests will use the\r
-            // API or URL defined in your initial proxy configuration.\r
-            store.proxy.conn.url = 'changed1.php';\r
-\r
-            // proxy URL will be superseded by API (only if proxy created to use ajax):\r
-            // It should be noted that proxy API changes are permanent and will\r
-            // be used for all subsequent requests.\r
-            store.proxy.api.load = 'changed2.php';\r
-\r
-            // However, altering the proxy API should be done using the public\r
-            // method <tt>{@link Ext.data.DataProxy#setApi setApi}</tt> instead.\r
-            store.proxy.setApi('load', 'changed2.php');\r
+            // Altering the proxy API should be done using the public\r
+            // method <tt>{@link Ext.data.DataProxy#setApi setApi}</tt>.\r
+            store.proxy.setApi('read', 'changed2.php');\r
 \r
             // Or set the entire API with a config-object.\r
             // When using the config-object option, you must redefine the <b>entire</b>\r
@@ -117,23 +124,17 @@ myStore.on({
      * </code></pre>\r
      * </p>\r
      */\r
-    // Prepare the proxy api.  Ensures all API-actions are defined with the Object-form.\r
-    try {\r
-        Ext.data.Api.prepare(this);\r
-    } catch (e) {\r
-        if (e instanceof Ext.data.Api.Error) {\r
-            e.toConsole();\r
-        }\r
-    }\r
 \r
     this.addEvents(\r
         /**\r
          * @event exception\r
-         * <p>Fires if an exception occurs in the Proxy during a remote request.\r
-         * This event is relayed through a corresponding\r
-         * {@link Ext.data.Store}.{@link Ext.data.Store#exception exception},\r
-         * so any Store instance may observe this event.\r
-         * This event can be fired for one of two reasons:</p>\r
+         * <p>Fires if an exception occurs in the Proxy during a remote request. This event is relayed\r
+         * through a corresponding {@link Ext.data.Store}.{@link Ext.data.Store#exception exception},\r
+         * so any Store instance may observe this event.</p>\r
+         * <p>In addition to being fired through the DataProxy instance that raised the event, this event is also fired\r
+         * through the Ext.data.DataProxy <i>class</i> to allow for centralized processing of exception events from <b>all</b>\r
+         * DataProxies by attaching a listener to the Ext.data.Proxy class itself.</p>\r
+         * <p>This event can be fired for one of two reasons:</p>\r
          * <div class="mdetail-params"><ul>\r
          * <li>remote-request <b>failed</b> : <div class="sub-desc">\r
          * The server did not return status === 200.\r
@@ -217,7 +218,10 @@ myStore.on({
         'loadexception',\r
         /**\r
          * @event beforewrite\r
-         * Fires before a request is generated for one of the actions Ext.data.Api.actions.create|update|destroy\r
+         * <p>Fires before a request is generated for one of the actions Ext.data.Api.actions.create|update|destroy</p>\r
+         * <p>In addition to being fired through the DataProxy instance that raised the event, this event is also fired\r
+         * through the Ext.data.DataProxy <i>class</i> to allow for centralized processing of beforewrite events from <b>all</b>\r
+         * DataProxies by attaching a listener to the Ext.data.Proxy class itself.</p>\r
          * @param {DataProxy} this The proxy for the request\r
          * @param {String} action [Ext.data.Api.actions.create|update|destroy]\r
          * @param {Record/Array[Record]} rs The Record(s) to create|update|destroy.\r
@@ -226,7 +230,10 @@ myStore.on({
         'beforewrite',\r
         /**\r
          * @event write\r
-         * Fires before the request-callback is called\r
+         * <p>Fires before the request-callback is called</p>\r
+         * <p>In addition to being fired through the DataProxy instance that raised the event, this event is also fired\r
+         * through the Ext.data.DataProxy <i>class</i> to allow for centralized processing of write events from <b>all</b>\r
+         * DataProxies by attaching a listener to the Ext.data.Proxy class itself.</p>\r
          * @param {DataProxy} this The proxy that sent the request\r
          * @param {String} action [Ext.data.Api.actions.create|upate|destroy]\r
          * @param {Object} data The data object extracted from the server-response\r
@@ -237,6 +244,17 @@ myStore.on({
         'write'\r
     );\r
     Ext.data.DataProxy.superclass.constructor.call(this);\r
+\r
+    // Prepare the proxy api.  Ensures all API-actions are defined with the Object-form.\r
+    try {\r
+        Ext.data.Api.prepare(this);\r
+    } catch (e) {\r
+        if (e instanceof Ext.data.Api.Error) {\r
+            e.toConsole();\r
+        }\r
+    }\r
+    // relay each proxy's events onto Ext.data.DataProxy class for centralized Proxy-listening\r
+    Ext.data.DataProxy.relayEvents(this, ['beforewrite', 'write', 'exception']);\r
 };\r
 \r
 Ext.extend(Ext.data.DataProxy, Ext.util.Observable, {\r
@@ -255,7 +273,7 @@ store: new Ext.data.Store({
     ...\r
 )}\r
      * </code></pre>\r
-     * There is no <code>{@link #api}</code> specified in the configuration of the proxy,\r
+     * If there is no <code>{@link #api}</code> specified in the configuration of the proxy,\r
      * all requests will be marshalled to a single RESTful url (/users) so the serverside\r
      * framework can inspect the HTTP Method and act accordingly:\r
      * <pre>\r
@@ -265,6 +283,18 @@ GET      /users     read
 PUT      /users/23  update\r
 DESTROY  /users/23  delete\r
      * </pre></p>\r
+     * <p>If set to <tt>true</tt>, a {@link Ext.data.Record#phantom non-phantom} record's\r
+     * {@link Ext.data.Record#id id} will be appended to the url. Some MVC (e.g., Ruby on Rails,\r
+     * Merb and Django) support segment based urls where the segments in the URL follow the\r
+     * Model-View-Controller approach:<pre><code>\r
+     * someSite.com/controller/action/id\r
+     * </code></pre>\r
+     * Where the segments in the url are typically:<div class="mdetail-params"><ul>\r
+     * <li>The first segment : represents the controller class that should be invoked.</li>\r
+     * <li>The second segment : represents the class function, or method, that should be called.</li>\r
+     * <li>The third segment : represents the ID (a variable typically passed to the method).</li>\r
+     * </ul></div></p>\r
+     * <br><p>Refer to <code>{@link Ext.data.DataProxy#api}</code> for additional information.</p>\r
      */\r
     restful: false,\r
 \r
@@ -380,11 +410,16 @@ proxy.setApi(Ext.data.Api.actions.read, '/users/new_load_url');
             throw new Ext.data.Api.Error('invalid-url', action);\r
         }\r
 \r
+        // look for urls having "provides" suffix (from Rails/Merb and others...),\r
+        // e.g.: /users.json, /users.xml, etc\r
+        // with restful routes, we need urls like:\r
+        // PUT /users/1.json\r
+        // DELETE /users/1.json\r
         var format = null;\r
-        var m = url.match(/(.*)(\.\w+)$/);  // <-- look for urls with "provides" suffix, e.g.: /users.json, /users.xml, etc\r
+        var m = url.match(/(.*)(\.json|\.xml|\.html)$/);\r
         if (m) {\r
-            format = m[2];\r
-            url = m[1];\r
+            format = m[2];  // eg ".json"\r
+            url = m[1];     // eg: "/users"\r
         }\r
         // prettyUrls is deprectated in favor of restful-config\r
         if ((this.prettyUrls === true || this.restful === true) && record instanceof Ext.data.Record && !record.phantom) {\r
@@ -404,6 +439,11 @@ proxy.setApi(Ext.data.Api.actions.read, '/users/new_load_url');
     }\r
 });\r
 \r
+// Apply the Observable prototype to the DataProxy class so that proxy instances can relay their\r
+// events to the class.  Allows for centralized listening of all proxy instances upon the DataProxy class.\r
+Ext.apply(Ext.data.DataProxy, Ext.util.Observable.prototype);\r
+Ext.util.Observable.call(Ext.data.DataProxy);\r
+\r
 /**\r
  * @class Ext.data.DataProxy.Error\r
  * @extends Ext.Error\r
@@ -425,3 +465,5 @@ Ext.apply(Ext.data.DataProxy.Error.prototype, {
         'api-invalid': 'Recieved an invalid API-configuration.  Please ensure your proxy API-configuration contains only the actions from Ext.data.Api.actions.'\r
     }\r
 });\r
+\r
+\r