~jstys-z/helioviewer.org/client5

« back to all changes in this revision

Viewing changes to lib/SunViewer/AjaxRequestWrapper.js

  • Committer: Michael Lynch
  • Date: 2008-02-01 06:21:07 UTC
  • Revision ID: mike@mike-desktop-20080201062107-uhqip0rcpsfc91mq
Initial import

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
var AjaxRequestWrapper = Class.create();
 
9
 
 
10
AjaxRequestWrapper.cache = {};
 
11
 
 
12
/**
 
13
 * @classDescription Wraps a GET url request to contain state
 
14
 * information like the url, and simplifies the use by setting some
 
15
 * standard behaviour.
 
16
 * TODO: Create a non-cached version for retrieving data that is
 
17
 * updated frequently.
 
18
 */
 
19
AjaxRequestWrapper.getCached = Class.create();
 
20
 
 
21
AjaxRequestWrapper.getCached.prototype = {
 
22
        /**
 
23
         * @constructor                                 Creates a new instance and processes the request.
 
24
         * @param {String} url                  The url to retrieve.
 
25
         * @param {Function} callback   The function that handles the retrieved data.
 
26
         */
 
27
        initialize: function(url, callback) {
 
28
                // Closures
 
29
                this.url = url;
 
30
                this.callback = callback;
 
31
                this.arguments = $A(arguments).slice(2);
 
32
 
 
33
                var self = this;
 
34
                
 
35
                if (AjaxRequestWrapper.cache[url]) {
 
36
                        callback.apply(null, $A([AjaxRequestWrapper.cache[self.url]]).concat(self.arguments));
 
37
                } else {
 
38
                        var onSuccess = function(transport) {
 
39
                                AjaxRequestWrapper.cache[self.url] = transport.responseText;
 
40
                                callback.apply(null, $A([AjaxRequestWrapper.cache[self.url]]).concat(self.arguments));
 
41
                        };
 
42
                        
 
43
                        var onFailure = function(transport) {
 
44
                                Debug.ajaxFailure(transport, self.url);
 
45
                        };
 
46
                        
 
47
                        new Ajax.Request(
 
48
                                url,
 
49
                                {
 
50
                                        method: 'get',
 
51
                                        onSuccess: onSuccess,
 
52
                                        onFailure: onFailure
 
53
                                }
 
54
                        );
 
55
                }
 
56
        }
 
57
};