~landscape/lazr-js/production

« back to all changes in this revision

Viewing changes to src-js/lazrjs/yui/jsonp/jsonp-debug.js

  • Committer: Sidnei da Silva
  • Date: 2010-09-18 14:54:13 UTC
  • mfrom: (166.11.12 toolchain)
  • Revision ID: sidnei.da.silva@canonical.com-20100918145413-8scojue3rodcm0f4
- Merge from lazr-js trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2010, Yahoo! Inc. All rights reserved.
 
3
Code licensed under the BSD License:
 
4
http://developer.yahoo.com/yui/license.html
 
5
version: 3.2.0
 
6
build: 2676
 
7
*/
 
8
YUI.add('jsonp', function(Y) {
 
9
 
 
10
var isFunction = Y.Lang.isFunction;
 
11
 
 
12
/**
 
13
 * <p>Provides a JSONPRequest class for repeated JSONP calls, and a convenience
 
14
 * method Y.jsonp(url, callback) to instantiate and send a JSONP request.</p>
 
15
 *
 
16
 * <p>Both the constructor as well as the convenience function take two
 
17
 * parameters: a url string and a callback.</p>
 
18
 *
 
19
 * <p>The url provided must include the placeholder string
 
20
 * &quot;{callback}&quot; which will be replaced by a dynamically
 
21
 * generated routing function to pass the data to your callback function.
 
22
 * An example url might look like
 
23
 * &quot;http://example.com/service?callback={callback}&quot;.</p>
 
24
 *
 
25
 * <p>The second parameter can be a callback function that accepts the JSON
 
26
 * payload as its argument, or a configuration object supporting the keys:</p>
 
27
 * <ul>
 
28
 *   <li>on - map of callback subscribers
 
29
 *      <ul>
 
30
 *         <li>success - function handler for successful transmission</li>
 
31
 *         <li>failure - function handler for failed transmission</li>
 
32
 *         <li>timeout - function handler for transactions that timeout</li>
 
33
 *      </ul>
 
34
 *  </li>
 
35
 *  <li>format  - override function for inserting the proxy name in the url</li>
 
36
 *  <li>timeout - the number of milliseconds to wait before giving up</li>
 
37
 *  <li>context - becomes <code>this</code> in the callbacks</li>
 
38
 *  <li>args    - array of subsequent parameters to pass to the callbacks</li>
 
39
 * </ul>
 
40
 *
 
41
 * @module jsonp
 
42
 * @class JSONPRequest
 
43
 * @constructor
 
44
 * @param url {String} the url of the JSONP service
 
45
 * @param callback {Object|Function} the default callback configuration or
 
46
 *                                   success handler
 
47
 */
 
48
function JSONPRequest() {
 
49
    this._init.apply(this, arguments);
 
50
}
 
51
 
 
52
JSONPRequest.prototype = {
 
53
    /**
 
54
     * Set up the success and failure handlers and the regex pattern used
 
55
     * to insert the temporary callback name in the url.
 
56
     *
 
57
     * @method _init
 
58
     * @param url {String} the url of the JSONP service
 
59
     * @param callback {Object|Function} Optional success callback or config
 
60
     *                  object containing success and failure functions and
 
61
     *                  the url regex.
 
62
     * @protected
 
63
     */
 
64
    _init : function (url, callback) {
 
65
        this.url = url;
 
66
 
 
67
        // Accept a function, an object, or nothing
 
68
        callback = (isFunction(callback)) ?
 
69
            { on: { success: callback } } :
 
70
            callback || {};
 
71
 
 
72
        var subs = callback.on || {};
 
73
 
 
74
        if (!subs.success) {
 
75
            subs.success = this._defaultCallback(url, callback);
 
76
        }
 
77
 
 
78
        // Apply defaults and store
 
79
        this._config = Y.merge({
 
80
                context: this,
 
81
                args   : [],
 
82
                format : this._format
 
83
            }, callback, { on: subs });
 
84
    },
 
85
 
 
86
    /** 
 
87
     * Override this method to provide logic to default the success callback if
 
88
     * it is not provided at construction.  This is overridden by jsonp-url to
 
89
     * parse the callback from the url string.
 
90
     * 
 
91
     * @method _defaultCallback
 
92
     * @param url {String} the url passed at construction
 
93
     * @param config {Object} (optional) the config object passed at
 
94
     *                        construction
 
95
     * @return {Function}
 
96
     */
 
97
    _defaultCallback: function () {},
 
98
 
 
99
    /** 
 
100
     * Issues the JSONP request.
 
101
     *
 
102
     * @method send
 
103
     * @param args* {any} any additional arguments to pass to the url formatter
 
104
     *              beyond the base url and the proxy function name
 
105
     * @chainable
 
106
     */
 
107
    send : function () {
 
108
        var args   = Y.Array(arguments, 0, true),
 
109
            proxy  = Y.guid(),
 
110
            config = this._config,
 
111
            url;
 
112
            
 
113
        args.unshift(this.url, 'YUI.Env.JSONP.' + proxy);
 
114
        url = config.format.apply(this, args);
 
115
 
 
116
        if (!config.on.success) {
 
117
            Y.log("No success handler defined.  Aborting JSONP request.", "warn", "jsonp");
 
118
            return this;
 
119
        }
 
120
 
 
121
        function wrap(fn) {
 
122
            return (isFunction(fn)) ?
 
123
                function (data) {
 
124
                    delete YUI.Env.JSONP[proxy];
 
125
                    fn.apply(config.context, [data].concat(config.args));
 
126
                } :
 
127
                null;
 
128
        }
 
129
 
 
130
        // Temporary un-sandboxed function alias
 
131
        // TODO: queuing
 
132
        YUI.Env.JSONP[proxy] = wrap(config.on.success);
 
133
 
 
134
        Y.Get.script(url, {
 
135
            onFailure: wrap(config.on.failure),
 
136
            onTimeout: wrap(config.on.timeout),
 
137
            timeout  : config.timeout
 
138
        });
 
139
 
 
140
        return this;
 
141
    },
 
142
 
 
143
    /**
 
144
     * Default url formatter.  Looks for callback= in the url and appends it
 
145
     * if not present.  The supplied proxy name will be assigned to the query
 
146
     * param.  Override this method by passing a function as the
 
147
     * &quot;format&quot; property in the config object to the constructor.
 
148
     *
 
149
     * @method _format
 
150
     * @param url { String } the original url
 
151
     * @param proxy {String} the function name that will be used as a proxy to
 
152
     *      the configured callback methods.
 
153
     * @param args* {any} additional args passed to send()
 
154
     * @return {String} fully qualified JSONP url
 
155
     * @protected
 
156
     */
 
157
    _format: function (url, proxy) {
 
158
        return url.replace(/\{callback\}/, proxy);
 
159
    }
 
160
};
 
161
 
 
162
Y.JSONPRequest = JSONPRequest;
 
163
 
 
164
/**
 
165
 *
 
166
 * @method Y.jsonp
 
167
 * @param url {String} the url of the JSONP service with the {callback}
 
168
 *          placeholder where the callback function name typically goes.
 
169
 * @param c {Function|Object} Callback function accepting the JSON payload
 
170
 *          as its argument, or a configuration object (see above).
 
171
 * @param args* {any} additional arguments to pass to send()
 
172
 * @return {JSONPRequest}
 
173
 * @static
 
174
 */
 
175
Y.jsonp = function (url,c) {
 
176
    var req = new Y.JSONPRequest(url,c);
 
177
    return req.send.apply(req, Y.Array(arguments, 2, true));
 
178
};
 
179
 
 
180
if (!YUI.Env.JSONP) {
 
181
    YUI.Env.JSONP = {};
 
182
}
 
183
 
 
184
 
 
185
}, '3.2.0' ,{requires:['get','oop']});