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