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