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