Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / form / action / DirectSubmit.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.DirectSubmit
17  * @extends Ext.form.action.Submit
18  * <p>Provides Ext.direct support for submitting form data.</p>
19  * <p>This example illustrates usage of Ext.direct.Direct to <b>submit</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     buttons:[{
28         text: 'Submit',
29         handler: function(){
30             myFormPanel.getForm().submit({
31                 params: {
32                     foo: 'bar',
33                     uid: 34
34                 }
35             });
36         }
37     }],
38
39     // configs apply to child items
40     defaults: {anchor: '100%'},
41     defaultType: 'textfield',
42     items: [{
43         fieldLabel: 'Name',
44         name: 'name'
45     },{
46         fieldLabel: 'Email',
47         name: 'email'
48     },{
49         fieldLabel: 'Company',
50         name: 'company'
51     }],
52
53     // configs for BasicForm
54     api: {
55         // The server-side method to call for load() requests
56         load: Profile.getBasicInfo,
57         // The server-side must mark the submit handler as a 'formHandler'
58         submit: Profile.updateBasicInfo
59     },
60     // specify the order for the passed params
61     paramOrder: ['uid', 'foo']
62 });
63  * </code></pre>
64  * The data packet sent to the server will resemble something like:
65  * <pre><code>
66 {
67     "action":"Profile","method":"updateBasicInfo","type":"rpc","tid":"6",
68     "result":{
69         "success":true,
70         "id":{
71             "extAction":"Profile","extMethod":"updateBasicInfo",
72             "extType":"rpc","extTID":"6","extUpload":"false",
73             "name":"Aaron Conran","email":"aaron@sencha.com","company":"Sencha Inc."
74         }
75     }
76 }
77  * </code></pre>
78  * The form will process a data packet returned by the server that is similar
79  * to the following:
80  * <pre><code>
81 // sample success packet (batched requests)
82 [
83     {
84         "action":"Profile","method":"updateBasicInfo","type":"rpc","tid":3,
85         "result":{
86             "success":true
87         }
88     }
89 ]
90
91 // sample failure packet (one request)
92 {
93         "action":"Profile","method":"updateBasicInfo","type":"rpc","tid":"6",
94         "result":{
95             "errors":{
96                 "email":"already taken"
97             },
98             "success":false,
99             "foo":"bar"
100         }
101 }
102  * </code></pre>
103  * Also see the discussion in {@link Ext.form.action.DirectLoad}.
104  */
105 Ext.define('Ext.form.action.DirectSubmit', {
106     extend:'Ext.form.action.Submit',
107     requires: ['Ext.direct.Manager'],
108     alternateClassName: 'Ext.form.Action.DirectSubmit',
109     alias: 'formaction.directsubmit',
110
111     type: 'directsubmit',
112
113     doSubmit: function() {
114         var me = this,
115             callback = Ext.Function.bind(me.onSuccess, me),
116             formEl = me.buildForm();
117         me.form.api.submit(formEl, callback, me);
118         Ext.removeNode(formEl);
119     },
120
121     // Direct actions have already been processed and therefore
122     // we can directly set the result; Direct Actions do not have
123     // a this.response property.
124     processResponse: function(result) {
125         return (this.result = result);
126     },
127
128     onSuccess: function(response, trans) {
129         if (trans.type === Ext.direct.Manager.self.exceptions.SERVER) {
130             response = {};
131         }
132         this.callParent([response]);
133     }
134 });
135