Upgrade to ExtJS 4.0.2 - Released 06/09/2011
[extjs.git] / src / form / action / Load.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.Load
17  * @extends Ext.form.action.Action
18  * <p>A class which handles loading of data from a server into the Fields of an {@link Ext.form.Basic}.</p>
19  * <p>Instances of this class are only created by a {@link Ext.form.Basic Form} when
20  * {@link Ext.form.Basic#load load}ing.</p>
21  * <p><u><b>Response Packet Criteria</b></u></p>
22  * <p>A response packet <b>must</b> contain:
23  * <div class="mdetail-params"><ul>
24  * <li><b><code>success</code></b> property : Boolean</li>
25  * <li><b><code>data</code></b> property : Object</li>
26  * <div class="sub-desc">The <code>data</code> property contains the values of Fields to load.
27  * The individual value object for each Field is passed to the Field's
28  * {@link Ext.form.field.Field#setValue setValue} method.</div></li>
29  * </ul></div>
30  * <p><u><b>JSON Packets</b></u></p>
31  * <p>By default, response packets are assumed to be JSON, so for the following form load call:<pre><code>
32 var myFormPanel = new Ext.form.Panel({
33     title: 'Client and routing info',
34     items: [{
35         fieldLabel: 'Client',
36         name: 'clientName'
37     }, {
38         fieldLabel: 'Port of loading',
39         name: 'portOfLoading'
40     }, {
41         fieldLabel: 'Port of discharge',
42         name: 'portOfDischarge'
43     }]
44 });
45 myFormPanel.{@link Ext.form.Panel#getForm getForm}().{@link Ext.form.Basic#load load}({
46     url: '/getRoutingInfo.php',
47     params: {
48         consignmentRef: myConsignmentRef
49     },
50     failure: function(form, action) {
51         Ext.Msg.alert("Load failed", action.result.errorMessage);
52     }
53 });
54 </code></pre>
55  * a <b>success response</b> packet may look like this:</p><pre><code>
56 {
57     success: true,
58     data: {
59         clientName: "Fred. Olsen Lines",
60         portOfLoading: "FXT",
61         portOfDischarge: "OSL"
62     }
63 }</code></pre>
64  * while a <b>failure response</b> packet may look like this:</p><pre><code>
65 {
66     success: false,
67     errorMessage: "Consignment reference not found"
68 }</code></pre>
69  * <p>Other data may be placed into the response for processing the {@link Ext.form.Basic Form}'s
70  * callback or event handler methods. The object decoded from this JSON is available in the
71  * {@link Ext.form.action.Action#result result} property.</p>
72  */
73 Ext.define('Ext.form.action.Load', {
74     extend:'Ext.form.action.Action',
75     requires: ['Ext.data.Connection'],
76     alternateClassName: 'Ext.form.Action.Load',
77     alias: 'formaction.load',
78
79     type: 'load',
80
81     /**
82      * @private
83      */
84     run: function() {
85         Ext.Ajax.request(Ext.apply(
86             this.createCallback(),
87             {
88                 method: this.getMethod(),
89                 url: this.getUrl(),
90                 headers: this.headers,
91                 params: this.getParams()
92             }
93         ));
94     },
95
96     /**
97      * @private
98      */
99     onSuccess: function(response){
100         var result = this.processResponse(response),
101             form = this.form;
102         if (result === true || !result.success || !result.data) {
103             this.failureType = Ext.form.action.Action.LOAD_FAILURE;
104             form.afterAction(this, false);
105             return;
106         }
107         form.clearInvalid();
108         form.setValues(result.data);
109         form.afterAction(this, true);
110     },
111
112     /**
113      * @private
114      */
115     handleResponse: function(response) {
116         var reader = this.form.reader,
117             rs, data;
118         if (reader) {
119             rs = reader.read(response);
120             data = rs.records && rs.records[0] ? rs.records[0].data : null;
121             return {
122                 success : rs.success,
123                 data : data
124             };
125         }
126         return Ext.decode(response.responseText);
127     }
128 });
129
130