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