Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / docs / source / DirectProxy.html
1 <html>
2 <head>
3   <title>The source code</title>
4     <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5     <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
6 </head>
7 <body  onload="prettyPrint();">
8     <pre class="prettyprint lang-js">/*!
9  * Ext JS Library 3.0.3
10  * Copyright(c) 2006-2009 Ext JS, LLC
11  * licensing@extjs.com
12  * http://www.extjs.com/license
13  */
14 <div id="cls-Ext.data.DirectProxy"></div>/**\r
15  * @class Ext.data.DirectProxy\r
16  * @extends Ext.data.DataProxy\r
17  */\r
18 Ext.data.DirectProxy = function(config){\r
19     Ext.apply(this, config);\r
20     if(typeof this.paramOrder == 'string'){\r
21         this.paramOrder = this.paramOrder.split(/[\s,|]/);\r
22     }\r
23     Ext.data.DirectProxy.superclass.constructor.call(this, config);\r
24 };\r
25 \r
26 Ext.extend(Ext.data.DirectProxy, Ext.data.DataProxy, {\r
27     <div id="cfg-Ext.data.DirectProxy-paramOrder"></div>/**\r
28      * @cfg {Array/String} paramOrder Defaults to <tt>undefined</tt>. A list of params to be executed\r
29      * server side.  Specify the params in the order in which they must be executed on the server-side\r
30      * as either (1) an Array of String values, or (2) a String of params delimited by either whitespace,\r
31      * comma, or pipe. For example,\r
32      * any of the following would be acceptable:<pre><code>\r
33 paramOrder: ['param1','param2','param3']\r
34 paramOrder: 'param1 param2 param3'\r
35 paramOrder: 'param1,param2,param3'\r
36 paramOrder: 'param1|param2|param'\r
37      </code></pre>\r
38      */\r
39     paramOrder: undefined,\r
40 \r
41     <div id="cfg-Ext.data.DirectProxy-paramsAsHash"></div>/**\r
42      * @cfg {Boolean} paramsAsHash\r
43      * Send parameters as a collection of named arguments (defaults to <tt>true</tt>). Providing a\r
44      * <tt>{@link #paramOrder}</tt> nullifies this configuration.\r
45      */\r
46     paramsAsHash: true,\r
47 \r
48     <div id="cfg-Ext.data.DirectProxy-directFn"></div>/**\r
49      * @cfg {Function} directFn\r
50      * Function to call when executing a request.  directFn is a simple alternative to defining the api configuration-parameter\r
51      * for Store's which will not implement a full CRUD api.\r
52      */\r
53     directFn : undefined,\r
54 \r
55     // protected\r
56     doRequest : function(action, rs, params, reader, callback, scope, options) {\r
57         var args = [];\r
58         var directFn = this.api[action] || this.directFn;\r
59 \r
60         switch (action) {\r
61             case Ext.data.Api.actions.create:\r
62                 args.push(params.jsonData[reader.meta.root]);           // <-- create(Hash)\r
63                 break;\r
64             case Ext.data.Api.actions.read:\r
65                 // If the method has no parameters, ignore the paramOrder/paramsAsHash.\r
66                 if(directFn.directCfg.method.len > 0){\r
67                     if(this.paramOrder){\r
68                         for(var i = 0, len = this.paramOrder.length; i < len; i++){\r
69                             args.push(params[this.paramOrder[i]]);\r
70                         }\r
71                     }else if(this.paramsAsHash){\r
72                         args.push(params);\r
73                     }\r
74                 }\r
75                 break;\r
76             case Ext.data.Api.actions.update:\r
77                 args.push(params.jsonData[reader.meta.root]);        // <-- update(Hash/Hash[])\r
78                 break;\r
79             case Ext.data.Api.actions.destroy:\r
80                 args.push(params.jsonData[reader.meta.root]);        // <-- destroy(Int/Int[])\r
81                 break;\r
82         }\r
83 \r
84         var trans = {\r
85             params : params || {},\r
86             request: {\r
87                 callback : callback,\r
88                 scope : scope,\r
89                 arg : options\r
90             },\r
91             reader: reader\r
92         };\r
93 \r
94         args.push(this.createCallback(action, rs, trans), this);\r
95         directFn.apply(window, args);\r
96     },\r
97 \r
98     // private\r
99     createCallback : function(action, rs, trans) {\r
100         return function(result, res) {\r
101             if (!res.status) {\r
102                 // @deprecated fire loadexception\r
103                 if (action === Ext.data.Api.actions.read) {\r
104                     this.fireEvent("loadexception", this, trans, res, null);\r
105                 }\r
106                 this.fireEvent('exception', this, 'remote', action, trans, res, null);\r
107                 trans.request.callback.call(trans.request.scope, null, trans.request.arg, false);\r
108                 return;\r
109             }\r
110             if (action === Ext.data.Api.actions.read) {\r
111                 this.onRead(action, trans, result, res);\r
112             } else {\r
113                 this.onWrite(action, trans, result, res, rs);\r
114             }\r
115         };\r
116     },\r
117     /**\r
118      * Callback for read actions\r
119      * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]\r
120      * @param {Object} trans The request transaction object\r
121      * @param {Object} res The server response\r
122      * @private\r
123      */\r
124     onRead : function(action, trans, result, res) {\r
125         var records;\r
126         try {\r
127             records = trans.reader.readRecords(result);\r
128         }\r
129         catch (ex) {\r
130             // @deprecated: Fire old loadexception for backwards-compat.\r
131             this.fireEvent("loadexception", this, trans, res, ex);\r
132 \r
133             this.fireEvent('exception', this, 'response', action, trans, res, ex);\r
134             trans.request.callback.call(trans.request.scope, null, trans.request.arg, false);\r
135             return;\r
136         }\r
137         this.fireEvent("load", this, res, trans.request.arg);\r
138         trans.request.callback.call(trans.request.scope, records, trans.request.arg, true);\r
139     },\r
140     /**\r
141      * Callback for write actions\r
142      * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]\r
143      * @param {Object} trans The request transaction object\r
144      * @param {Object} res The server response\r
145      * @private\r
146      */\r
147     onWrite : function(action, trans, result, res, rs) {\r
148         var data = trans.reader.extractData(result);\r
149         this.fireEvent("write", this, action, data, res, rs, trans.request.arg);\r
150         trans.request.callback.call(trans.request.scope, data, res, true);\r
151     }\r
152 });\r
153 \r
154 </pre>
155 </body>
156 </html>