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