Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / form / action / DirectLoad.js
1 /**
2  * @class Ext.form.action.DirectLoad
3  * @extends Ext.form.action.Load
4  * <p>Provides {@link Ext.direct.Manager} support for loading form data.</p>
5  * <p>This example illustrates usage of Ext.direct.Direct to <b>load</b> a form through Ext.Direct.</p>
6  * <pre><code>
7 var myFormPanel = new Ext.form.Panel({
8     // configs for FormPanel
9     title: 'Basic Information',
10     renderTo: document.body,
11     width: 300, height: 160,
12     padding: 10,
13
14     // configs apply to child items
15     defaults: {anchor: '100%'},
16     defaultType: 'textfield',
17     items: [{
18         fieldLabel: 'Name',
19         name: 'name'
20     },{
21         fieldLabel: 'Email',
22         name: 'email'
23     },{
24         fieldLabel: 'Company',
25         name: 'company'
26     }],
27
28     // configs for BasicForm
29     api: {
30         // The server-side method to call for load() requests
31         load: Profile.getBasicInfo,
32         // The server-side must mark the submit handler as a 'formHandler'
33         submit: Profile.updateBasicInfo
34     },
35     // specify the order for the passed params
36     paramOrder: ['uid', 'foo']
37 });
38
39 // load the form
40 myFormPanel.getForm().load({
41     // pass 2 arguments to server side getBasicInfo method (len=2)
42     params: {
43         foo: 'bar',
44         uid: 34
45     }
46 });
47  * </code></pre>
48  * The data packet sent to the server will resemble something like:
49  * <pre><code>
50 [
51     {
52         "action":"Profile","method":"getBasicInfo","type":"rpc","tid":2,
53         "data":[34,"bar"] // note the order of the params
54     }
55 ]
56  * </code></pre>
57  * The form will process a data packet returned by the server that is similar
58  * to the following format:
59  * <pre><code>
60 [
61     {
62         "action":"Profile","method":"getBasicInfo","type":"rpc","tid":2,
63         "result":{
64             "success":true,
65             "data":{
66                 "name":"Fred Flintstone",
67                 "company":"Slate Rock and Gravel",
68                 "email":"fred.flintstone@slaterg.com"
69             }
70         }
71     }
72 ]
73  * </code></pre>
74  */
75 Ext.define('Ext.form.action.DirectLoad', {
76     extend:'Ext.form.action.Load',
77     requires: ['Ext.direct.Manager'],
78     alternateClassName: 'Ext.form.Action.DirectLoad',
79     alias: 'formaction.directload',
80
81     type: 'directload',
82
83     run: function() {
84         this.form.api.load.apply(window, this.getArgs());
85     },
86
87     /**
88      * @private
89      * Build the arguments to be sent to the Direct call.
90      * @return Array
91      */
92     getArgs: function() {
93         var me = this,
94             args = [],
95             form = me.form,
96             paramOrder = form.paramOrder,
97             params = me.getParams(),
98             i, len;
99
100         // If a paramOrder was specified, add the params into the argument list in that order.
101         if (paramOrder) {
102             for (i = 0, len = paramOrder.length; i < len; i++) {
103                 args.push(params[paramOrder[i]]);
104             }
105         }
106         // If paramsAsHash was specified, add all the params as a single object argument.
107         else if (form.paramsAsHash) {
108             args.push(params);
109         }
110
111         // Add the callback and scope to the end of the arguments list
112         args.push(me.onSuccess, me);
113
114         return args;
115     },
116
117     // Direct actions have already been processed and therefore
118     // we can directly set the result; Direct Actions do not have
119     // a this.response property.
120     processResponse: function(result) {
121         return (this.result = result);
122     },
123
124     onSuccess: function(result, trans) {
125         if (trans.type == Ext.direct.Manager.self.exceptions.SERVER) {
126             result = {};
127         }
128         this.callParent([result]);
129     }
130 });
131
132