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 */
28
29 Pman.Request = function(config){
30     
31     Pman.Request.superclass.constructor.call(this, config);
32     this.request(config);
33 }
34
35 Roo.extend(Pman.Request, Roo.data.Connection, {
36     
37     processResponse : function(response) {
38         
39         var res = '';
40         try {
41             res = Roo.decode(response.responseText);
42             // oops...
43             if (typeof(res) != 'object') {
44                 res = { success : false, errorMsg : res, errors : true };
45             }
46             if (typeof(res.success) == 'undefined') {
47                 res.success = false;
48             }
49             
50         } catch(e) {
51             res = { success : false,  errorMsg : response.responseText, errors : true };
52         }
53         return res;
54     },
55     
56     handleResponse : function(response){
57        this.transId = false;
58        var options = response.argument.options;
59        response.argument = options ? options.argument : null;
60        this.fireEvent("requestcomplete", this, response, options);
61        
62         var res = this.processResponse(response);
63                 
64         if (!res.success) { // error!
65             if (options.failure) {
66                 // failure is handled... - do not show error..
67                 if (true === Roo.callback(options.failure, options.scope, [res, options])) {
68                     return;
69                 }
70             }
71             Roo.MessageBox.hide(); // hide any existing messages..
72             Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg : "Error Sending");
73             return;
74         }
75         Roo.callback(options.success, options.scope, [res, options]);
76         
77     },
78     handleFailure : function(response, e){
79         this.transId = false;
80         var options = response.argument.options;
81         response.argument = options ? options.argument : null;
82         this.fireEvent("requestexception", this, response, options, e);
83         Roo.callback(options.failure, options.scope, [response, options]);
84         Roo.callback(options.callback, options.scope, [options, false, response]);
85         if (!options.failure) {
86             Roo.MessageBox.hide(); // hide any existing messages..
87             Roo.MessageBox.alert("Error", res.errorMsg ? res.errorMsg : "Error Sending");
88         }
89     }
90 });