Pman.Request.js
[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 */
29
30 Pman.Request = function(config){
31     
32     Pman.Request.superclass.constructor.call(this, config);
33     
34     if (this.mask) {
35         this.maskEl = this.maskEl || Roo.get(document.body);
36         Roo.log('mask EL !!!!!!!!!!!!!!!!!!!!!!!!!!11');
37         Roo.log(this.maskEl);
38         Roo.get(this.maskEl).mask(this.mask);
39         
40     }
41     this.request(config);
42
43 }
44
45 Roo.extend(Pman.Request, Roo.data.Connection, {
46     // private
47     processResponse : function(response) {
48         // convert the Roo Connection response into JSON data.
49         
50         var res;
51         try {
52             res = Roo.decode(response.responseText);
53             // oops...
54             if (typeof(res) != 'object') {
55                 res = { success : false, errorMsg : res, errors : true };
56             }
57             if (typeof(res.success) == 'undefined') {
58                 res.success = false;
59             }
60             
61         } catch(e) {
62             res = { success : false,  errorMsg : response.responseText || Roo.encode(response), errors : true };
63         }
64         //Roo.log(response.responseText);
65         if (!res.success && !res.errorMsg) {
66             res.errorMsg = Roo.encode(response);
67         }
68         return res;
69     },
70     
71     handleResponse : function(response)
72     {
73         this.transId = false;
74         var options = response.argument.options;
75         response.argument = options ? options.argument : null;
76         this.fireEvent("requestcomplete", this, response, options);
77         
78         if (this.mask && this.maskEl) {
79             Roo.get(this.maskEl).unmask(false);
80         }
81         var res = this.processResponse(response);
82                 
83         if (!res.success) { // error!
84             if (options.failure) {
85                 // failure is handled... - do not show error..
86                 Roo.callback(options.failure, options.scope, [res, options]);
87                 return;
88             }
89             Roo.MessageBox.hide(); // hide any existing messages..
90             Roo.MessageBox.alert("Error", res && res.errorMsg ?  res.errorMsg : "Error Sending data");
91             return;
92         }
93         Roo.callback(options.success, options.scope, [res, options]);
94         
95     },
96     handleFailure : function(response, e){
97         this.transId = false;
98         var options = response.argument.options;
99         response.argument = options ? options.argument : null;
100         this.fireEvent("requestexception", this, response, options, e);
101         var res = Roo.callback(options.failure, options.scope, [response, options]);
102         if (this.mask && this.maskEl) {
103             Roo.get(this.maskEl).unmask(true);
104         }
105         if (res !== true) {
106             var decode = this.processResponse(response);
107             Roo.log(decode);
108             if (Roo.MessageBox.isVisible()) {
109                 alert(decode && decode.errorMsg ?  decode.errorMsg : "Error Sending data - return true from failure to remove message");
110                 return;
111             }            
112             Roo.MessageBox.alert("Error", decode && decode.errorMsg ?  decode.errorMsg : "Error Sending data");
113         }
114     }
115 });