3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js"><div id="cls-Ext.form.Action"></div>/**
10 * @class Ext.form.Action
11 * <p>The subclasses of this class provide actions to perform upon {@link Ext.form.BasicForm Form}s.</p>
12 * <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when
13 * the Form needs to perform an action such as submit or load. The Configuration options
14 * listed for this class are set through the Form's action methods: {@link Ext.form.BasicForm#submit submit},
15 * {@link Ext.form.BasicForm#load load} and {@link Ext.form.BasicForm#doAction doAction}</p>
16 * <p>The instance of Action which performed the action is passed to the success
17 * and failure callbacks of the Form's action methods ({@link Ext.form.BasicForm#submit submit},
18 * {@link Ext.form.BasicForm#load load} and {@link Ext.form.BasicForm#doAction doAction}),
19 * and to the {@link Ext.form.BasicForm#actioncomplete actioncomplete} and
20 * {@link Ext.form.BasicForm#actionfailed actionfailed} event handlers.</p>
22 Ext.form.Action = function(form, options){
24 this.options = options || {};
27 <div id="prop-Ext.form.Action-CLIENT_INVALID"></div>/**
28 * Failure type returned when client side validation of the Form fails
29 * thus aborting a submit action. Client side validation is performed unless
30 * {@link #clientValidation} is explicitly set to <tt>false</tt>.
34 Ext.form.Action.CLIENT_INVALID = 'client';
35 <div id="prop-Ext.form.Action-SERVER_INVALID"></div>/**
36 * <p>Failure type returned when server side processing fails and the {@link #result}'s
37 * <tt style="font-weight:bold">success</tt> property is set to <tt>false</tt>.</p>
38 * <p>In the case of a form submission, field-specific error messages may be returned in the
39 * {@link #result}'s <tt style="font-weight:bold">errors</tt> property.</p>
43 Ext.form.Action.SERVER_INVALID = 'server';
44 <div id="prop-Ext.form.Action-CONNECT_FAILURE"></div>/**
45 * Failure type returned when a communication error happens when attempting
46 * to send a request to the remote server. The {@link #response} may be examined to
47 * provide further information.
51 Ext.form.Action.CONNECT_FAILURE = 'connect';
52 <div id="prop-Ext.form.Action-LOAD_FAILURE"></div>/**
53 * Failure type returned when the response's <tt style="font-weight:bold">success</tt>
54 * property is set to <tt>false</tt>, or no field values are returned in the response's
55 * <tt style="font-weight:bold">data</tt> property.
59 Ext.form.Action.LOAD_FAILURE = 'load';
61 Ext.form.Action.prototype = {
62 <div id="cfg-Ext.form.Action-url"></div>/**
63 * @cfg {String} url The URL that the Action is to invoke.
65 <div id="cfg-Ext.form.Action-reset"></div>/**
66 * @cfg {Boolean} reset When set to <tt><b>true</b></tt>, causes the Form to be
67 * {@link Ext.form.BasicForm.reset reset} on Action success. If specified, this happens
68 * <b>before</b> the {@link #success} callback is called and before the Form's
69 * {@link Ext.form.BasicForm.actioncomplete actioncomplete} event fires.
71 <div id="cfg-Ext.form.Action-method"></div>/**
72 * @cfg {String} method The HTTP method to use to access the requested URL. Defaults to the
73 * {@link Ext.form.BasicForm}'s method, or if that is not specified, the underlying DOM form's method.
75 <div id="cfg-Ext.form.Action-params"></div>/**
76 * @cfg {Mixed} params <p>Extra parameter values to pass. These are added to the Form's
77 * {@link Ext.form.BasicForm#baseParams} and passed to the specified URL along with the Form's
79 * <p>Parameters are encoded as standard HTTP parameters using {@link Ext#urlEncode}.</p>
81 <div id="cfg-Ext.form.Action-timeout"></div>/**
82 * @cfg {Number} timeout The number of seconds to wait for a server response before
83 * failing with the {@link #failureType} as {@link #Action.CONNECT_FAILURE}. If not specified,
84 * defaults to the configured <tt>{@link Ext.form.BasicForm#timeout timeout}</tt> of the
85 * {@link Ext.form.BasicForm form}.
87 <div id="cfg-Ext.form.Action-success"></div>/**
88 * @cfg {Function} success The function to call when a valid success return packet is recieved.
89 * The function is passed the following parameters:<ul class="mdetail-params">
90 * <li><b>form</b> : Ext.form.BasicForm<div class="sub-desc">The form that requested the action</div></li>
91 * <li><b>action</b> : Ext.form.Action<div class="sub-desc">The Action class. The {@link #result}
92 * property of this object may be examined to perform custom postprocessing.</div></li>
95 <div id="cfg-Ext.form.Action-failure"></div>/**
96 * @cfg {Function} failure The function to call when a failure packet was recieved, or when an
97 * error ocurred in the Ajax communication.
98 * The function is passed the following parameters:<ul class="mdetail-params">
99 * <li><b>form</b> : Ext.form.BasicForm<div class="sub-desc">The form that requested the action</div></li>
100 * <li><b>action</b> : Ext.form.Action<div class="sub-desc">The Action class. If an Ajax
101 * error ocurred, the failure type will be in {@link #failureType}. The {@link #result}
102 * property of this object may be examined to perform custom postprocessing.</div></li>
105 <div id="cfg-Ext.form.Action-scope"></div>/**
106 * @cfg {Object} scope The scope in which to call the callback functions (The <tt>this</tt> reference
107 * for the callback functions).
109 <div id="cfg-Ext.form.Action-waitMsg"></div>/**
110 * @cfg {String} waitMsg The message to be displayed by a call to {@link Ext.MessageBox#wait}
111 * during the time the action is being processed.
113 <div id="cfg-Ext.form.Action-waitTitle"></div>/**
114 * @cfg {String} waitTitle The title to be displayed by a call to {@link Ext.MessageBox#wait}
115 * during the time the action is being processed.
118 <div id="prop-Ext.form.Action-type"></div>/**
119 * The type of action this Action instance performs.
120 * Currently only "submit" and "load" are supported.
124 <div id="prop-Ext.form.Action-failureType"></div>/**
125 * The type of failure detected will be one of these: {@link #CLIENT_INVALID},
126 * {@link #SERVER_INVALID}, {@link #CONNECT_FAILURE}, or {@link #LOAD_FAILURE}. Usage:
128 var fp = new Ext.form.FormPanel({
134 if(fp.getForm().isValid()){
135 fp.getForm().submit({
136 url: 'form-submit.php',
137 waitMsg: 'Submitting your data...',
138 success: function(form, action){
139 // server responded with success = true
140 var result = action.{@link #result};
142 failure: function(form, action){
143 if (action.{@link #failureType} === Ext.form.Action.{@link #CONNECT_FAILURE}) {
144 Ext.Msg.alert('Error',
145 'Status:'+action.{@link #response}.status+': '+
146 action.{@link #response}.statusText);
148 if (action.failureType === Ext.form.Action.{@link #SERVER_INVALID}){
149 // server responded with success = false
150 Ext.Msg.alert('Invalid', action.{@link #result}.errormsg);
159 fp.getForm().reset();
163 * @property failureType
166 <div id="prop-Ext.form.Action-response"></div>/**
167 * The XMLHttpRequest object used to perform the action.
171 <div id="prop-Ext.form.Action-result"></div>/**
172 * The decoded response object containing a boolean <tt style="font-weight:bold">success</tt> property and
173 * other, action-specific properties.
179 run : function(options){
184 success : function(response){
189 handleResponse : function(response){
193 // default connection failure
194 failure : function(response){
195 this.response = response;
196 this.failureType = Ext.form.Action.CONNECT_FAILURE;
197 this.form.afterAction(this, false);
201 // shared code among all Actions to validate that there was a response
202 // with either responseText or responseXml
203 processResponse : function(response){
204 this.response = response;
205 if(!response.responseText && !response.responseXML){
208 this.result = this.handleResponse(response);
212 // utility functions used internally
213 getUrl : function(appendParams){
214 var url = this.options.url || this.form.url || this.form.el.dom.action;
216 var p = this.getParams();
218 url = Ext.urlAppend(url, p);
225 getMethod : function(){
226 return (this.options.method || this.form.method || this.form.el.dom.method || 'POST').toUpperCase();
230 getParams : function(){
231 var bp = this.form.baseParams;
232 var p = this.options.params;
234 if(typeof p == "object"){
235 p = Ext.urlEncode(Ext.applyIf(p, bp));
236 }else if(typeof p == 'string' && bp){
237 p += '&' + Ext.urlEncode(bp);
240 p = Ext.urlEncode(bp);
246 createCallback : function(opts){
247 var opts = opts || {};
249 success: this.success,
250 failure: this.failure,
252 timeout: (opts.timeout*1000) || (this.form.timeout*1000),
253 upload: this.form.fileUpload ? this.success : undefined
258 <div id="cls-Ext.form.Action.Submit"></div>/**
259 * @class Ext.form.Action.Submit
260 * @extends Ext.form.Action
261 * <p>A class which handles submission of data from {@link Ext.form.BasicForm Form}s
262 * and processes the returned response.</p>
263 * <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when
264 * {@link Ext.form.BasicForm#submit submit}ting.</p>
265 * <p><u><b>Response Packet Criteria</b></u></p>
266 * <p>A response packet may contain:
267 * <div class="mdetail-params"><ul>
268 * <li><b><code>success</code></b> property : Boolean
269 * <div class="sub-desc">The <code>success</code> property is required.</div></li>
270 * <li><b><code>errors</code></b> property : Object
271 * <div class="sub-desc"><div class="sub-desc">The <code>errors</code> property,
272 * which is optional, contains error messages for invalid fields.</div></li>
274 * <p><u><b>JSON Packets</b></u></p>
275 * <p>By default, response packets are assumed to be JSON, so a typical response
276 * packet may look like this:</p><pre><code>
280 clientCode: "Client not found",
281 portOfLoading: "This field must not be null"
284 * <p>Other data may be placed into the response for processing by the {@link Ext.form.BasicForm}'s callback
285 * or event handler methods. The object decoded from this JSON is available in the
286 * {@link Ext.form.Action#result result} property.</p>
287 * <p>Alternatively, if an {@link #errorReader} is specified as an {@link Ext.data.XmlReader XmlReader}:</p><pre><code>
288 errorReader: new Ext.data.XmlReader({
296 * <p>then the results may be sent back in XML format:</p><pre><code>
297 <?xml version="1.0" encoding="UTF-8"?>
298 <message success="false">
301 <id>clientCode</id>
302 <msg><![CDATA[Code not found. <br /><i>This is a test validation message from the server </i>]]></msg>
305 <id>portOfLoading</id>
306 <msg><![CDATA[Port not found. <br /><i>This is a test validation message from the server </i>]]></msg>
311 * <p>Other elements may be placed into the response XML for processing by the {@link Ext.form.BasicForm}'s callback
312 * or event handler methods. The XML document is available in the {@link #errorReader}'s {@link Ext.data.XmlReader#xmlData xmlData} property.</p>
314 Ext.form.Action.Submit = function(form, options){
315 Ext.form.Action.Submit.superclass.constructor.call(this, form, options);
318 Ext.extend(Ext.form.Action.Submit, Ext.form.Action, {
319 <div id="cfg-Ext.form.Action.Submit-errorReader"></div>/**
320 * @cfg {Ext.data.DataReader} errorReader <p><b>Optional. JSON is interpreted with
321 * no need for an errorReader.</b></p>
322 * <p>A Reader which reads a single record from the returned data. The DataReader's
323 * <b>success</b> property specifies how submission success is determined. The Record's
324 * data provides the error messages to apply to any invalid form Fields.</p>
326 <div id="cfg-Ext.form.Action.Submit-clientValidation"></div>/**
327 * @cfg {boolean} clientValidation Determines whether a Form's fields are validated
328 * in a final call to {@link Ext.form.BasicForm#isValid isValid} prior to submission.
329 * Pass <tt>false</tt> in the Form's submit options to prevent this. If not defined, pre-submission field validation
336 var o = this.options;
337 var method = this.getMethod();
338 var isGet = method == 'GET';
339 if(o.clientValidation === false || this.form.isValid()){
340 Ext.Ajax.request(Ext.apply(this.createCallback(o), {
341 form:this.form.el.dom,
342 url:this.getUrl(isGet),
345 params:!isGet ? this.getParams() : null,
346 isUpload: this.form.fileUpload
348 }else if (o.clientValidation !== false){ // client validation failed
349 this.failureType = Ext.form.Action.CLIENT_INVALID;
350 this.form.afterAction(this, false);
355 success : function(response){
356 var result = this.processResponse(response);
357 if(result === true || result.success){
358 this.form.afterAction(this, true);
362 this.form.markInvalid(result.errors);
364 this.failureType = Ext.form.Action.SERVER_INVALID;
365 this.form.afterAction(this, false);
369 handleResponse : function(response){
370 if(this.form.errorReader){
371 var rs = this.form.errorReader.read(response);
374 for(var i = 0, len = rs.records.length; i < len; i++) {
375 var r = rs.records[i];
379 if(errors.length < 1){
383 success : rs.success,
387 return Ext.decode(response.responseText);
392 <div id="cls-Ext.form.Action.Load"></div>/**
393 * @class Ext.form.Action.Load
394 * @extends Ext.form.Action
395 * <p>A class which handles loading of data from a server into the Fields of an {@link Ext.form.BasicForm}.</p>
396 * <p>Instances of this class are only created by a {@link Ext.form.BasicForm Form} when
397 * {@link Ext.form.BasicForm#load load}ing.</p>
398 * <p><u><b>Response Packet Criteria</b></u></p>
399 * <p>A response packet <b>must</b> contain:
400 * <div class="mdetail-params"><ul>
401 * <li><b><code>success</code></b> property : Boolean</li>
402 * <li><b><code>data</code></b> property : Object</li>
403 * <div class="sub-desc">The <code>data</code> property contains the values of Fields to load.
404 * The individual value object for each Field is passed to the Field's
405 * {@link Ext.form.Field#setValue setValue} method.</div></li>
407 * <p><u><b>JSON Packets</b></u></p>
408 * <p>By default, response packets are assumed to be JSON, so for the following form load call:<pre><code>
409 var myFormPanel = new Ext.form.FormPanel({
410 title: 'Client and routing info',
412 fieldLabel: 'Client',
415 fieldLabel: 'Port of loading',
416 name: 'portOfLoading'
418 fieldLabel: 'Port of discharge',
419 name: 'portOfDischarge'
422 myFormPanel.{@link Ext.form.FormPanel#getForm getForm}().{@link Ext.form.BasicForm#load load}({
423 url: '/getRoutingInfo.php',
425 consignmentRef: myConsignmentRef
427 failure: function(form, action) {
428 Ext.Msg.alert("Load failed", action.result.errorMessage);
432 * a <b>success response</b> packet may look like this:</p><pre><code>
436 clientName: "Fred. Olsen Lines",
437 portOfLoading: "FXT",
438 portOfDischarge: "OSL"
441 * while a <b>failure response</b> packet may look like this:</p><pre><code>
444 errorMessage: "Consignment reference not found"
446 * <p>Other data may be placed into the response for processing the {@link Ext.form.BasicForm Form}'s
447 * callback or event handler methods. The object decoded from this JSON is available in the
448 * {@link Ext.form.Action#result result} property.</p>
450 Ext.form.Action.Load = function(form, options){
451 Ext.form.Action.Load.superclass.constructor.call(this, form, options);
452 this.reader = this.form.reader;
455 Ext.extend(Ext.form.Action.Load, Ext.form.Action, {
461 Ext.Ajax.request(Ext.apply(
462 this.createCallback(this.options), {
463 method:this.getMethod(),
464 url:this.getUrl(false),
465 headers: this.options.headers,
466 params:this.getParams()
471 success : function(response){
472 var result = this.processResponse(response);
473 if(result === true || !result.success || !result.data){
474 this.failureType = Ext.form.Action.LOAD_FAILURE;
475 this.form.afterAction(this, false);
478 this.form.clearInvalid();
479 this.form.setValues(result.data);
480 this.form.afterAction(this, true);
484 handleResponse : function(response){
485 if(this.form.reader){
486 var rs = this.form.reader.read(response);
487 var data = rs.records && rs.records[0] ? rs.records[0].data : null;
489 success : rs.success,
493 return Ext.decode(response.responseText);
499 <div id="cls-Ext.form.Action.DirectLoad"></div>/**
500 * @class Ext.form.Action.DirectLoad
501 * @extends Ext.form.Action.Load
502 * <p>Provides Ext.direct support for loading form data.</p>
503 * <p>This example illustrates usage of Ext.Direct to <b>load</b> a form through Ext.Direct.</p>
505 var myFormPanel = new Ext.form.FormPanel({
506 // configs for FormPanel
507 title: 'Basic Information',
508 renderTo: document.body,
509 width: 300, height: 160,
512 // configs apply to child items
513 defaults: {anchor: '100%'},
514 defaultType: 'textfield',
522 fieldLabel: 'Company',
526 // configs for BasicForm
528 // The server-side method to call for load() requests
529 load: Profile.getBasicInfo,
530 // The server-side must mark the submit handler as a 'formHandler'
531 submit: Profile.updateBasicInfo
533 // specify the order for the passed params
534 paramOrder: ['uid', 'foo']
538 myFormPanel.getForm().load({
539 // pass 2 arguments to server side getBasicInfo method (len=2)
546 * The data packet sent to the server will resemble something like:
550 "action":"Profile","method":"getBasicInfo","type":"rpc","tid":2,
551 "data":[34,"bar"] // note the order of the params
555 * The form will process a data packet returned by the server that is similar
556 * to the following format:
560 "action":"Profile","method":"getBasicInfo","type":"rpc","tid":2,
564 "name":"Fred Flintstone",
565 "company":"Slate Rock and Gravel",
566 "email":"fred.flintstone@slaterg.com"
573 Ext.form.Action.DirectLoad = Ext.extend(Ext.form.Action.Load, {
574 constructor: function(form, opts) {
575 Ext.form.Action.DirectLoad.superclass.constructor.call(this, form, opts);
580 var args = this.getParams();
581 args.push(this.success, this);
582 this.form.api.load.apply(window, args);
585 getParams : function() {
586 var buf = [], o = {};
587 var bp = this.form.baseParams;
588 var p = this.options.params;
590 var paramOrder = this.form.paramOrder;
592 for(var i = 0, len = paramOrder.length; i < len; i++){
593 buf.push(o[paramOrder[i]]);
595 }else if(this.form.paramsAsHash){
600 // Direct actions have already been processed and therefore
601 // we can directly set the result; Direct Actions do not have
602 // a this.response property.
603 processResponse : function(result) {
604 this.result = result;
608 success : function(response, trans){
609 if(trans.type == Ext.Direct.exceptions.SERVER){
612 Ext.form.Action.DirectLoad.superclass.success.call(this, response);
616 <div id="cls-Ext.form.Action.DirectSubmit"></div>/**
617 * @class Ext.form.Action.DirectSubmit
618 * @extends Ext.form.Action.Submit
619 * <p>Provides Ext.direct support for submitting form data.</p>
620 * <p>This example illustrates usage of Ext.Direct to <b>submit</b> a form through Ext.Direct.</p>
622 var myFormPanel = new Ext.form.FormPanel({
623 // configs for FormPanel
624 title: 'Basic Information',
625 renderTo: document.body,
626 width: 300, height: 160,
631 myFormPanel.getForm().submit({
640 // configs apply to child items
641 defaults: {anchor: '100%'},
642 defaultType: 'textfield',
650 fieldLabel: 'Company',
654 // configs for BasicForm
656 // The server-side method to call for load() requests
657 load: Profile.getBasicInfo,
658 // The server-side must mark the submit handler as a 'formHandler'
659 submit: Profile.updateBasicInfo
661 // specify the order for the passed params
662 paramOrder: ['uid', 'foo']
665 * The data packet sent to the server will resemble something like:
668 "action":"Profile","method":"updateBasicInfo","type":"rpc","tid":"6",
672 "extAction":"Profile","extMethod":"updateBasicInfo",
673 "extType":"rpc","extTID":"6","extUpload":"false",
674 "name":"Aaron Conran","email":"aaron@extjs.com","company":"Ext JS, LLC"
679 * The form will process a data packet returned by the server that is similar
682 // sample success packet (batched requests)
685 "action":"Profile","method":"updateBasicInfo","type":"rpc","tid":3,
692 // sample failure packet (one request)
694 "action":"Profile","method":"updateBasicInfo","type":"rpc","tid":"6",
697 "email":"already taken"
704 * Also see the discussion in {@link Ext.form.Action.DirectLoad}.
706 Ext.form.Action.DirectSubmit = Ext.extend(Ext.form.Action.Submit, {
707 constructor : function(form, opts) {
708 Ext.form.Action.DirectSubmit.superclass.constructor.call(this, form, opts);
710 type : 'directsubmit',
711 // override of Submit
713 var o = this.options;
714 if(o.clientValidation === false || this.form.isValid()){
715 // tag on any additional params to be posted in the
717 this.success.params = this.getParams();
718 this.form.api.submit(this.form.el.dom, this.success, this);
719 }else if (o.clientValidation !== false){ // client validation failed
720 this.failureType = Ext.form.Action.CLIENT_INVALID;
721 this.form.afterAction(this, false);
725 getParams : function() {
727 var bp = this.form.baseParams;
728 var p = this.options.params;
732 // Direct actions have already been processed and therefore
733 // we can directly set the result; Direct Actions do not have
734 // a this.response property.
735 processResponse : function(result) {
736 this.result = result;
740 success : function(response, trans){
741 if(trans.type == Ext.Direct.exceptions.SERVER){
744 Ext.form.Action.DirectSubmit.superclass.success.call(this, response);
748 Ext.form.Action.ACTION_TYPES = {
749 'load' : Ext.form.Action.Load,
750 'submit' : Ext.form.Action.Submit,
751 'directload' : Ext.form.Action.DirectLoad,
752 'directsubmit' : Ext.form.Action.DirectSubmit