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     failure : function () {
17          
18     }
19 });
20
21 </code></pre>
22
23 * @constructor
24 * @param {Object} cfg   Configuration object.
25 * @cfg {String} url     Location to download from.
26 * @cfg {String} method     GET or POST (default GET), POST will create a form, and post that into the hidden frame.
27 * @cfg  {Object/String/Function} params (Optional) An object containing properties which are used as parameters to the
28 *       request, a url encoded string or a function to call to get either.
29 * @cfg  {Function} success  called with ( JSON decoded data of the data.. )
30 */
31
32 Pman.Request = function(config){
33     
34     Pman.Request.superclass.constructor.call(this, config);
35     this.request(config);
36 }
37
38 Roo.extend(Pman.Request, Roo.data.Connection, {
39     
40     processResponse : function(response) {
41         
42         var res = '';
43         try {
44             res = Roo.decode(response.responseText);
45             // oops...
46             if (typeof(res) != 'object') {
47                 res = { success : false, errorMsg : res, errors : true };
48             }
49             if (typeof(res.success) == 'undefined') {
50                 res.success = false;
51             }
52             
53         } catch(e) {
54             res = { success : false,  errorMsg : response.responseText, errors : true };
55         }
56         return res;
57     },
58     
59     handleResponse : function(response){
60        this.transId = false;
61        var options = response.argument.options;
62        response.argument = options ? options.argument : null;
63        this.fireEvent("requestcomplete", this, response, options);
64        
65         var res = this.processResponse(response);
66                 
67         if (!res.success) { // error!
68             if (options.failure) {
69                 // failure is handled... - do not show error..
70                 if (true === Roo.callback(options.failure, options.scope, [res, options])) {
71                     return;
72                 }
73             }
74             Roo.MessageBox.hide(); // hide any existing messages..
75             Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg : "Error Sending");
76             return;
77         }
78         Roo.callback(options.success, options.scope, [res, options]);
79         
80     },
81     handleFailure : function(response, e){
82         this.transId = false;
83         var options = response.argument.options;
84         response.argument = options ? options.argument : null;
85         this.fireEvent("requestexception", this, response, options, e);
86         Roo.callback(options.failure, options.scope, [response, options]);
87         Roo.callback(options.callback, options.scope, [options, false, response]);
88         if (!options.failure) {
89             Roo.MessageBox.hide(); // hide any existing messages..
90             Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg : "Error Sending");
91         }
92     }
93 });