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