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