Upgrade to ExtJS 3.0.3 - Released 10/11/2009
[extjs.git] / src / data / DirectProxy.js
1 /*!
2  * Ext JS Library 3.0.3
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     // protected\r
49     doRequest : function(action, rs, params, reader, callback, scope, options) {\r
50         var args = [];\r
51         var directFn = this.api[action] || this.directFn;\r
52 \r
53         switch (action) {\r
54             case Ext.data.Api.actions.create:\r
55                 args.push(params.jsonData[reader.meta.root]);           // <-- create(Hash)\r
56                 break;\r
57             case Ext.data.Api.actions.read:\r
58                 // If the method has no parameters, ignore the paramOrder/paramsAsHash.\r
59                 if(directFn.directCfg.method.len > 0){\r
60                     if(this.paramOrder){\r
61                         for(var i = 0, len = this.paramOrder.length; i < len; i++){\r
62                             args.push(params[this.paramOrder[i]]);\r
63                         }\r
64                     }else if(this.paramsAsHash){\r
65                         args.push(params);\r
66                     }\r
67                 }\r
68                 break;\r
69             case Ext.data.Api.actions.update:\r
70                 args.push(params.jsonData[reader.meta.root]);        // <-- update(Hash/Hash[])\r
71                 break;\r
72             case Ext.data.Api.actions.destroy:\r
73                 args.push(params.jsonData[reader.meta.root]);        // <-- destroy(Int/Int[])\r
74                 break;\r
75         }\r
76 \r
77         var trans = {\r
78             params : params || {},\r
79             request: {\r
80                 callback : callback,\r
81                 scope : scope,\r
82                 arg : options\r
83             },\r
84             reader: reader\r
85         };\r
86 \r
87         args.push(this.createCallback(action, rs, trans), this);\r
88         directFn.apply(window, args);\r
89     },\r
90 \r
91     // private\r
92     createCallback : function(action, rs, trans) {\r
93         return function(result, res) {\r
94             if (!res.status) {\r
95                 // @deprecated fire loadexception\r
96                 if (action === Ext.data.Api.actions.read) {\r
97                     this.fireEvent("loadexception", this, trans, res, null);\r
98                 }\r
99                 this.fireEvent('exception', this, 'remote', action, trans, res, null);\r
100                 trans.request.callback.call(trans.request.scope, null, trans.request.arg, false);\r
101                 return;\r
102             }\r
103             if (action === Ext.data.Api.actions.read) {\r
104                 this.onRead(action, trans, result, res);\r
105             } else {\r
106                 this.onWrite(action, trans, result, res, rs);\r
107             }\r
108         };\r
109     },\r
110     /**\r
111      * Callback for read actions\r
112      * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]\r
113      * @param {Object} trans The request transaction object\r
114      * @param {Object} res The server response\r
115      * @private\r
116      */\r
117     onRead : function(action, trans, result, res) {\r
118         var records;\r
119         try {\r
120             records = trans.reader.readRecords(result);\r
121         }\r
122         catch (ex) {\r
123             // @deprecated: Fire old loadexception for backwards-compat.\r
124             this.fireEvent("loadexception", this, trans, res, ex);\r
125 \r
126             this.fireEvent('exception', this, 'response', action, trans, res, ex);\r
127             trans.request.callback.call(trans.request.scope, null, trans.request.arg, false);\r
128             return;\r
129         }\r
130         this.fireEvent("load", this, res, trans.request.arg);\r
131         trans.request.callback.call(trans.request.scope, records, trans.request.arg, true);\r
132     },\r
133     /**\r
134      * Callback for write actions\r
135      * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]\r
136      * @param {Object} trans The request transaction object\r
137      * @param {Object} res The server response\r
138      * @private\r
139      */\r
140     onWrite : function(action, trans, result, res, rs) {\r
141         var data = trans.reader.extractData(result);\r
142         this.fireEvent("write", this, action, data, res, rs, trans.request.arg);\r
143         trans.request.callback.call(trans.request.scope, data, res, true);\r
144     }\r
145 });\r
146 \r