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