typo
[Pman.Core] / Pman.Request.js
1 //<script type="text/javascript">
2 /**
3 * @class Pman.Request
4 * Handles generic requests  - an extension of Roo.data.Connection that runs the request
5 * on construction. shows error messages, and parses results.
6 * Usage:
7 <pre><code>
8 var t = new Pman.Request({
9     url: baseURL + '/Images/Download/0/myfile.jpg',
10     params: { .... },
11     success : function(res) {
12         Roo.log(res.data);
13         Roo.log(res.total);
14         ....
15     } 
16 });
17
18 </code></pre>
19
20 * @constructor
21 * @param {Object} cfg   Configuration object.
22 * @cfg {String} url     Location to download from.
23 * @cfg {String} method     GET or POST (default GET), POST will create a form, and post that into the hidden frame.
24 * @cfg  {Object/String/Function} params (Optional) An object containing properties which are used as parameters to the
25 *       request, a url encoded string or a function to call to get either.
26 * @cfg  {Function} success  called with ( JSON decoded data of the data.. )
27 * @cfg  {Function} success  called with ( JSON decoded data of the data.. )
28 * @cfg {Boolean} showFailureDialog (true|false) default true
29 */
30
31 Pman.Request = function(config){
32     
33     Pman.Request.superclass.constructor.call(this, config);
34     
35     if (this.mask) {
36         this.maskEl = this.maskEl || Roo.get(document.body);
37         Roo.get(this.maskEl).mask(this.mask);
38         
39     }
40     this.request(config);
41
42 }
43
44 Roo.extend(Pman.Request, Roo.data.Connection, {
45     // private
46     showFailureDialog : true,
47     
48     processResponse : function(response) {
49         // convert the Roo Connection response into JSON data.
50         
51         var res;
52         try {
53             res = Roo.decode(response.responseText);
54             // oops...
55             if (typeof(res) != 'object') {
56                 res = { success : false, errorMsg : res, errors : true };
57             }
58             if (typeof(res.success) == 'undefined') {
59                 res.success = false;
60             }
61             
62         } catch(e) {
63             res = { success : false,  errorMsg : response.responseText || Roo.encode(response), errors : true };
64         }
65         //Roo.log(response.responseText);
66         if (!res.success && !res.errorMsg) {
67             res.errorMsg = Roo.encode(response);
68         }
69         return res;
70     },
71     
72     handleResponse : function(response)
73     {
74         this.transId = false;
75         var options = response.argument.options;
76         response.argument = options ? options.argument : null;
77         this.fireEvent("requestcomplete", this, response, options);
78         
79         if (this.mask && this.maskEl) {
80             Roo.get(this.maskEl).unmask(true);
81         }
82         var res = this.processResponse(response);
83                 
84         if (!res.success && !res.done) { // error!
85             if (options.failure) {
86                 // failure is handled... - do not show error..
87                 Roo.callback(options.failure, options.scope, [res, options]);
88                 return;
89             }
90             Roo.MessageBox.hide(); // hide any existing messages..
91             Roo.MessageBox.alert("Error", res && res.errorMsg ?  res.errorMsg : "Error Sending data");
92             return;
93         }
94         Roo.callback(options.success, options.scope, [res, options]);
95         
96     },
97     
98     handleFailure : function(response, e){
99         this.transId = false;
100         var options = response.argument.options;
101         response.argument = options ? options.argument : null;
102         this.fireEvent("requestexception", this, response, options, e);
103         var res = Roo.callback(options.failure, options.scope, [response, options]);
104         if (this.mask && this.maskEl) {
105             Roo.get(this.maskEl).unmask(true);
106         }
107         if(!this.showFailureDialog){
108             return;
109         }
110         if (res !== true) {
111             var decode = this.processResponse(response);
112             Roo.log(decode);
113             // this pops up even if we have a method to handle failure..
114             if (Roo.MessageBox.isVisible() ) {
115                 if (!options.failure) {
116                     alert(decode && decode.errorMsg ?  decode.errorMsg : "Error Sending data - return true from failure to remove message");
117                 }
118                 
119                 return;
120             }
121             
122             Roo.MessageBox.alert("Error", decode && decode.errorMsg ?  decode.errorMsg : "Error Sending data");
123             
124         }
125     }
126 });