3 * Copyright(c) 2006-2010 Sencha Inc.
5 * http://www.sencha.com/license
8 * @class Ext.data.DirectProxy
9 * @extends Ext.data.DataProxy
11 Ext.data.DirectProxy = function(config){
12 Ext.apply(this, config);
13 if(typeof this.paramOrder == 'string'){
14 this.paramOrder = this.paramOrder.split(/[\s,|]/);
16 Ext.data.DirectProxy.superclass.constructor.call(this, config);
19 Ext.extend(Ext.data.DirectProxy, Ext.data.DataProxy, {
21 * @cfg {Array/String} paramOrder Defaults to <tt>undefined</tt>. A list of params to be executed
22 * server side. Specify the params in the order in which they must be executed on the server-side
23 * as either (1) an Array of String values, or (2) a String of params delimited by either whitespace,
24 * comma, or pipe. For example,
25 * any of the following would be acceptable:<pre><code>
26 paramOrder: ['param1','param2','param3']
27 paramOrder: 'param1 param2 param3'
28 paramOrder: 'param1,param2,param3'
29 paramOrder: 'param1|param2|param'
32 paramOrder: undefined,
35 * @cfg {Boolean} paramsAsHash
36 * Send parameters as a collection of named arguments (defaults to <tt>true</tt>). Providing a
37 * <tt>{@link #paramOrder}</tt> nullifies this configuration.
42 * @cfg {Function} directFn
43 * Function to call when executing a request. directFn is a simple alternative to defining the api configuration-parameter
44 * for Store's which will not implement a full CRUD api.
49 * DirectProxy implementation of {@link Ext.data.DataProxy#doRequest}
50 * @param {String} action The crud action type (create, read, update, destroy)
51 * @param {Ext.data.Record/Ext.data.Record[]} rs If action is load, rs will be null
52 * @param {Object} params An object containing properties which are to be used as HTTP parameters
53 * for the request to the remote server.
54 * @param {Ext.data.DataReader} reader The Reader object which converts the data
55 * object into a block of Ext.data.Records.
56 * @param {Function} callback
57 * <div class="sub-desc"><p>A function to be called after the request.
58 * The <tt>callback</tt> is passed the following arguments:<ul>
59 * <li><tt>r</tt> : Ext.data.Record[] The block of Ext.data.Records.</li>
60 * <li><tt>options</tt>: Options object from the action request</li>
61 * <li><tt>success</tt>: Boolean success indicator</li></ul></p></div>
62 * @param {Object} scope The scope (<code>this</code> reference) in which the callback function is executed. Defaults to the browser window.
63 * @param {Object} arg An optional argument which is passed to the callback as its second parameter.
66 doRequest : function(action, rs, params, reader, callback, scope, options) {
68 directFn = this.api[action] || this.directFn;
71 case Ext.data.Api.actions.create:
72 args.push(params.jsonData); // <-- create(Hash)
74 case Ext.data.Api.actions.read:
75 // If the method has no parameters, ignore the paramOrder/paramsAsHash.
76 if(directFn.directCfg.method.len > 0){
78 for(var i = 0, len = this.paramOrder.length; i < len; i++){
79 args.push(params[this.paramOrder[i]]);
81 }else if(this.paramsAsHash){
86 case Ext.data.Api.actions.update:
87 args.push(params.jsonData); // <-- update(Hash/Hash[])
89 case Ext.data.Api.actions.destroy:
90 args.push(params.jsonData); // <-- destroy(Int/Int[])
95 params : params || {},
104 args.push(this.createCallback(action, rs, trans), this);
105 directFn.apply(window, args);
109 createCallback : function(action, rs, trans) {
111 return function(result, res) {
113 // @deprecated fire loadexception
114 if (action === Ext.data.Api.actions.read) {
115 me.fireEvent("loadexception", me, trans, res, null);
117 me.fireEvent('exception', me, 'remote', action, trans, res, null);
118 trans.request.callback.call(trans.request.scope, null, trans.request.arg, false);
121 if (action === Ext.data.Api.actions.read) {
122 me.onRead(action, trans, result, res);
124 me.onWrite(action, trans, result, res, rs);
130 * Callback for read actions
131 * @param {String} action [Ext.data.Api.actions.create|read|update|destroy]
132 * @param {Object} trans The request transaction object
133 * @param {Object} result Data object picked out of the server-response.
134 * @param {Object} res The server response
137 onRead : function(action, trans, result, res) {
140 records = trans.reader.readRecords(result);
143 // @deprecated: Fire old loadexception for backwards-compat.
144 this.fireEvent("loadexception", this, trans, res, ex);
146 this.fireEvent('exception', this, 'response', action, trans, res, ex);
147 trans.request.callback.call(trans.request.scope, null, trans.request.arg, false);
150 this.fireEvent("load", this, res, trans.request.arg);
151 trans.request.callback.call(trans.request.scope, records, trans.request.arg, true);
154 * Callback for write actions
155 * @param {String} action [{@link Ext.data.Api#actions create|read|update|destroy}]
156 * @param {Object} trans The request transaction object
157 * @param {Object} result Data object picked out of the server-response.
158 * @param {Object} res The server response
159 * @param {Ext.data.Record/[Ext.data.Record]} rs The Store resultset associated with the action.
162 onWrite : function(action, trans, result, res, rs) {
163 var data = trans.reader.extractData(trans.reader.getRoot(result), false);
164 var success = trans.reader.getSuccess(result);
165 success = (success !== false);
167 this.fireEvent("write", this, action, data, res, rs, trans.request.arg);
169 this.fireEvent('exception', this, 'remote', action, trans, result, rs);
171 trans.request.callback.call(trans.request.scope, data, res, success);