1 | /*! |
---|
2 | * Ext JS Library 3.4.0 |
---|
3 | * Copyright(c) 2006-2011 Sencha Inc. |
---|
4 | * licensing@sencha.com |
---|
5 | * http://www.sencha.com/license |
---|
6 | */ |
---|
7 | /** |
---|
8 | * @class Ext.data.DirectProxy |
---|
9 | * @extends Ext.data.DataProxy |
---|
10 | */ |
---|
11 | Ext.data.DirectProxy = function(config){ |
---|
12 | Ext.apply(this, config); |
---|
13 | if(typeof this.paramOrder == 'string'){ |
---|
14 | this.paramOrder = this.paramOrder.split(/[\s,|]/); |
---|
15 | } |
---|
16 | Ext.data.DirectProxy.superclass.constructor.call(this, config); |
---|
17 | }; |
---|
18 | |
---|
19 | Ext.extend(Ext.data.DirectProxy, Ext.data.DataProxy, { |
---|
20 | /** |
---|
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' |
---|
30 | </code></pre> |
---|
31 | */ |
---|
32 | paramOrder: undefined, |
---|
33 | |
---|
34 | /** |
---|
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. |
---|
38 | */ |
---|
39 | paramsAsHash: true, |
---|
40 | |
---|
41 | /** |
---|
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. |
---|
45 | */ |
---|
46 | directFn : undefined, |
---|
47 | |
---|
48 | /** |
---|
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. |
---|
64 | * @protected |
---|
65 | */ |
---|
66 | doRequest : function(action, rs, params, reader, callback, scope, options) { |
---|
67 | var args = [], |
---|
68 | directFn = this.api[action] || this.directFn; |
---|
69 | |
---|
70 | switch (action) { |
---|
71 | case Ext.data.Api.actions.create: |
---|
72 | args.push(params.jsonData); // <-- create(Hash) |
---|
73 | break; |
---|
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){ |
---|
77 | if(this.paramOrder){ |
---|
78 | for(var i = 0, len = this.paramOrder.length; i < len; i++){ |
---|
79 | args.push(params[this.paramOrder[i]]); |
---|
80 | } |
---|
81 | }else if(this.paramsAsHash){ |
---|
82 | args.push(params); |
---|
83 | } |
---|
84 | } |
---|
85 | break; |
---|
86 | case Ext.data.Api.actions.update: |
---|
87 | args.push(params.jsonData); // <-- update(Hash/Hash[]) |
---|
88 | break; |
---|
89 | case Ext.data.Api.actions.destroy: |
---|
90 | args.push(params.jsonData); // <-- destroy(Int/Int[]) |
---|
91 | break; |
---|
92 | } |
---|
93 | |
---|
94 | var trans = { |
---|
95 | params : params || {}, |
---|
96 | request: { |
---|
97 | callback : callback, |
---|
98 | scope : scope, |
---|
99 | arg : options |
---|
100 | }, |
---|
101 | reader: reader |
---|
102 | }; |
---|
103 | |
---|
104 | args.push(this.createCallback(action, rs, trans), this); |
---|
105 | directFn.apply(window, args); |
---|
106 | }, |
---|
107 | |
---|
108 | // private |
---|
109 | createCallback : function(action, rs, trans) { |
---|
110 | var me = this; |
---|
111 | return function(result, res) { |
---|
112 | if (!res.status) { |
---|
113 | // @deprecated fire loadexception |
---|
114 | if (action === Ext.data.Api.actions.read) { |
---|
115 | me.fireEvent("loadexception", me, trans, res, null); |
---|
116 | } |
---|
117 | me.fireEvent('exception', me, 'remote', action, trans, res, null); |
---|
118 | trans.request.callback.call(trans.request.scope, null, trans.request.arg, false); |
---|
119 | return; |
---|
120 | } |
---|
121 | if (action === Ext.data.Api.actions.read) { |
---|
122 | me.onRead(action, trans, result, res); |
---|
123 | } else { |
---|
124 | me.onWrite(action, trans, result, res, rs); |
---|
125 | } |
---|
126 | }; |
---|
127 | }, |
---|
128 | |
---|
129 | /** |
---|
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 |
---|
135 | * @protected |
---|
136 | */ |
---|
137 | onRead : function(action, trans, result, res) { |
---|
138 | var records; |
---|
139 | try { |
---|
140 | records = trans.reader.readRecords(result); |
---|
141 | } |
---|
142 | catch (ex) { |
---|
143 | // @deprecated: Fire old loadexception for backwards-compat. |
---|
144 | this.fireEvent("loadexception", this, trans, res, ex); |
---|
145 | |
---|
146 | this.fireEvent('exception', this, 'response', action, trans, res, ex); |
---|
147 | trans.request.callback.call(trans.request.scope, null, trans.request.arg, false); |
---|
148 | return; |
---|
149 | } |
---|
150 | this.fireEvent("load", this, res, trans.request.arg); |
---|
151 | trans.request.callback.call(trans.request.scope, records, trans.request.arg, true); |
---|
152 | }, |
---|
153 | /** |
---|
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. |
---|
160 | * @protected |
---|
161 | */ |
---|
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); |
---|
166 | if (success){ |
---|
167 | this.fireEvent("write", this, action, data, res, rs, trans.request.arg); |
---|
168 | }else{ |
---|
169 | this.fireEvent('exception', this, 'remote', action, trans, result, rs); |
---|
170 | } |
---|
171 | trans.request.callback.call(trans.request.scope, data, res, success); |
---|
172 | } |
---|
173 | }); |
---|