~jstys-z/helioviewer.org/client5

« back to all changes in this revision

Viewing changes to lib/helioviewer/LEGACY/AjaxRequestWrapper.js

  • Committer: Keith Hughitt
  • Date: 2010-02-17 22:00:59 UTC
  • mfrom: (402.1.68 hv)
  • Revision ID: keith.hughitt@nasa.gov-20100217220059-wmdq7kgokj4seryx
Merged with Keith's branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * @author Patrick Schmiedel patrick.schmiedel@gmx.net
3
 
 */
4
 
 
5
 
/**
6
 
 * @classDescription Static class. Keeps a cache of retrieved urls.
7
 
 */
8
 
 
9
 
/*global Class, Control, document, $, $A, Ajax, Debug */
10
 
var AjaxRequestWrapper = Class.create();
11
 
 
12
 
AjaxRequestWrapper.cache = {};
13
 
 
14
 
/**
15
 
 * @classDescription Wraps a GET url request to contain state
16
 
 * information like the url, and simplifies the use by setting some
17
 
 * standard behaviour.
18
 
 * TODO: Create a non-cached version for retrieving data that is
19
 
 * updated frequently.
20
 
 */
21
 
AjaxRequestWrapper.getCached = Class.create();
22
 
 
23
 
AjaxRequestWrapper.getCached.prototype = {
24
 
        /**
25
 
         * @constructor                                 Creates a new instance and processes the request.
26
 
         * @param {String} url                  The url to retrieve.
27
 
         * @param {Function} callback   The function that handles the retrieved data.
28
 
         */
29
 
        initialize: function (url, callback) {
30
 
 
31
 
                // Closures
32
 
                this.url = url;
33
 
                this.callback = callback;
34
 
                
35
 
                // Get any additional arguments for XHR. The first two items of the array are
36
 
                // the url and callback function for the XHR. In many cases this.arguments will
37
 
                // simply eval to [].
38
 
                this.args = $A(arguments).slice(2);
39
 
 
40
 
                var self = this;
41
 
                
42
 
                if (AjaxRequestWrapper.cache[url]) {
43
 
                        callback.apply(null, $A([AjaxRequestWrapper.cache[self.url]]).concat(self.args));
44
 
                } else {
45
 
                        var onSuccess = function (transport) {
46
 
                                AjaxRequestWrapper.cache[self.url] = transport.responseText;
47
 
                                callback.apply(null, $A([AjaxRequestWrapper.cache[self.url]]).concat(self.args));
48
 
                        };
49
 
                        
50
 
                        var onFailure = function (transport) {
51
 
                                Debug.ajaxFailure(transport, self.url);
52
 
                        };
53
 
                        
54
 
                        var trash = new Ajax.Request(
55
 
                                url,
56
 
                                {
57
 
                                        method: 'get',
58
 
                                        onSuccess: onSuccess,
59
 
                                        onFailure: onFailure
60
 
                                }
61
 
                        );
62
 
                }
63
 
        }
64
 
};