Skip to content

Commit

Permalink
Gallery Build Tag: gallery-2010.02.10-01
Browse files Browse the repository at this point in the history
  • Loading branch information
YUI Builder committed Feb 10, 2010
1 parent d435f42 commit 4556a62
Show file tree
Hide file tree
Showing 6 changed files with 485 additions and 154 deletions.
216 changes: 139 additions & 77 deletions build/gallery-jsonp/gallery-jsonp-debug.js
@@ -1,40 +1,46 @@
YUI.add('gallery-jsonp', function(Y) {

var l = Y.Lang,
noop = function () {};
var isObject = Y.Lang.isObject,
isFunction = Y.Lang.isFunction;

/**
* <p>Provides a JSONPRequest class for repeated JSONP calls, and a convenience
* method Y.jsonp(url, callback) to instantiate and send a JSONP request.</p>
*
* <p>The url of the JSONP service will have a proxy function assigned as the
* callback name. By default it will look for callback=(anything). If
* (anything) is present, it will be clobbered by the proxy name. And if the
* callback= param can't be found, it will be appended to the url.</p>
* <p>The callback for the response can be named in the url explicitly or
* provided in the configuration (second parameter to the constructor).
*
* <p>To override this behavior, pass a function to the &quot;format&quot;
* property in the callback config.</p>
* <p>By default, the query parameter string &quot;callback=???&quot; will be
* searched for in the url (??? can be anything). If it's not found, it will
* be added on. If the JSONP service uses a different parameter name or url
* format, you can override this behavior with the <code>format</code> property
* in the callback config.</p>
*
* <p>The second parameter can be a callback function that accepts the JSON
* payload as its argument, or a configuration object supporting the keys:</p>
* <ul>
* <li>success - function handler for successful transmission</li>
* <li>failure - function handler for failed transmission</li>
* <li>format - function for inserting the proxy name into the url</li>
* <li>on - map of callback subscribers
* <ul>
* <li>success - function handler for successful transmission</li>
* <li>failure - function handler for failed transmission</li>
* <li>timeout - function handler for transactions that timeout</li>
* </ul>
* </li>
* <li>format - override function for inserting the proxy name in the url</li>
* <li>timeout - the number of milliseconds to wait before giving up</li>
* <li>context - becomes <code>this</code> in the callbacks</li>
* <li>args - array of subsequent parameters to pass to the callbacks</li>
* </ul>
*
* @module gallery-jsonp
* @class JSONPRequest
* @constructor
* @param url {String} the url of the JSONP service
* @param callback {Object|Function} the callback configuration or success
* handler
* @param callback {Object|Function} the default callback configuration or
* success handler
*/
function JSONPRequest(url,callback) {

this.url = url;

this._init(callback);
function JSONPRequest() {
this._init.apply( this, arguments );
}

/**
Expand All @@ -48,7 +54,7 @@ function JSONPRequest(url,callback) {
* @protected
* @static
*/
JSONPRequest._pattern = /\bcallback=.*?(?=&|$)/i;
JSONPRequest._pattern = /\bcallback=(.*?)(?=&|$)/i;

/**
* Template used by the default URL formatter to add the callback function name
Expand All @@ -68,53 +74,79 @@ JSONPRequest.prototype = {
* to insert the temporary callback name in the url.
*
* @method _init
* @param cfg {Object|Function} The success callback or the config
* @param url {String} the url of the JSONP service
* @param callback {Object|Function} Optional success callback or config
* object containing success and failure functions and
* the url regex.
* @protected
*/
_init : function (cfg) {
var c = l.isObject(cfg) ? cfg : {};
_init : function ( url, callback ) {
this.url = url;

this.success = l.isFunction(c) ?
c :
l.isFunction(c.success) ?
c.success :
noop;
callback = callback || {};

if ( isFunction( callback ) ) {
callback = { on: { success: callback } };
}

this.failure = l.isFunction(c.failure) ? c.failure : noop;
callback.on = callback.on || {};

if (l.isFunction(c.format)) {
this._format = c.format;
if ( !callback.on.success ) {
callback.on.success = this._getCallbackFromUrl( url );
}

// Apply defaults and store
this._config = Y.merge( {
on : {},
context: this,
args : [],
format : this._format
}, callback );

},

/**
* Default url formatter. Looks for callback= in the url and appends it
* if not present. The supplied proxy name will be assigned to the query
* param. Override this method by passing a function as the
* &quot;format&quot; property in the config object to the constructor.
* <p>Parses the url for a callback named explicitly in the string.
* Override this if the target JSONP service uses a different query
* parameter or url format.</p>
*
* @method _format
* @param proxy {String} the function name that will be used as a proxy to
* the configured callback methods.
* @return {String} fully qualified JSONP url
* <p>If the callback is declared inline, the corresponding function will
* be returned. Otherwise null.</p>
*
* @method _getCallbackFromUrl
* @param url { String } the url to search in
* @return { Function } the callback function if found, or null
* @protected
*/
_format: function (proxy) {
var url = this.url,
callback = JSONPRequest._template.replace(/\{callback\}/, proxy),
c;
_getCallbackFromUrl: function ( url ) {
var match = url.match( JSONPRequest._pattern ),
callback, context, bits, i;

if (JSONPRequest._pattern.test(url)) {
return url.replace(JSONPRequest._pattern, callback);
} else {
c = url.slice(-1);
if (c !== '&' && c !== '?') {
url += (url.indexOf('?') > -1) ? '&' : '?';
if ( match ) {
// resolve from the global
context = Y.config.win;

// callback=foo.bar.func => [ 'func', 'bar', 'foo' ]
// @TODO doesn't support bracket notation (callback=foo["bar"].func)
bits = match[1].split( /\./ ).reverse();

callback = bits.shift();

for ( i = bits.length - 1; i >= 0; --i ) {
context = context[ bits[ i ] ];
if ( !isObject( context ) ) {
return null;
}
}

if ( isObject( context ) && isFunction( context[ callback ] ) ) {
// bind to preserve context declared inline, so
// callback=foo.bar.func => 'this' is foo.bar in func
return Y.bind( context[ callback ], context );
}
return url + callback;
}

return null;
},

/**
Expand All @@ -123,35 +155,66 @@ JSONPRequest.prototype = {
* @method send
* @chainable
*/
send : function () {
var proxy = Y.guid().replace(/-/g,'_'),
url = this._format('YUI.' + proxy),
success = this.success,
failure = this.failure;
send : function ( callback ) {
if ( !this._config.on.success ) {
Y.log( "No success handler defined. Aborting JSONP request.", "warn", "jsonp" );
return this;
}

var proxy = Y.guid().replace( /-/g, '_' ),
config = this._config,
url = config.format.call( this,
this.url, 'YUI.Env.JSONP.' + proxy );

function wrap( fn ) {
return ( isFunction( fn ) ) ?
function ( data ) {
delete YUI.Env.JSONP[ proxy ];
fn.apply( config.context, [ data ].concat( config.args ) );
} :
null;
}

// Temporary un-sandboxed function alias
YUI[proxy] = success;

// Use the YUI instance's Get util to add the script and trigger the
// callback.
YUI({ modules: { _ : { fullpath : url } } }).
use('_', function(X,res) {
delete YUI[proxy];

var el = Y.Selector.query('head > script[src*='+proxy+']',null,true);
if (el) {
el.parentNode.removeChild(el);
} else {
Y.log('JSONP script element not found for cleanup','warn','jsonp');
}
YUI.Env.JSONP[ proxy ] = wrap( config.on.success );

if (!res.success) {
failure(url);
}
});
Y.Get.script( url, {
onFailure: wrap( config.on.failure ),
onTimeout: wrap( config.on.timeout || config.on.failure ),
timeout : config.timeout
} );

return this;
},

/**
* Default url formatter. Looks for callback= in the url and appends it
* if not present. The supplied proxy name will be assigned to the query
* param. Override this method by passing a function as the
* &quot;format&quot; property in the config object to the constructor.
*
* @method _format
* @param url { String } the original url
* @param proxy {String} the function name that will be used as a proxy to
* the configured callback methods.
* @return {String} fully qualified JSONP url
* @protected
*/
_format: function ( url, proxy ) {
var callback = JSONPRequest._template.replace(/\{callback\}/, proxy),
lastChar;

if (JSONPRequest._pattern.test(url)) {
return url.replace(JSONPRequest._pattern, callback);
} else {
lastChar = url.slice(-1);
if (lastChar !== '&' && lastChar !== '?') {
url += (url.indexOf('?') > -1) ? '&' : '?';
}
return url + callback;
}
}

};

Y.JSONPRequest = JSONPRequest;
Expand All @@ -167,11 +230,10 @@ Y.JSONPRequest = JSONPRequest;
* @static
*/
Y.jsonp = function (url,c) {
var req = new Y.JSONPRequest(url,c);

// returns null if invalid inputs
return req && req.send();
return new Y.JSONPRequest(url,c).send();
};

YUI.Env.JSONP = {};


}, 'gallery-2009.10.27' ,{requires:['selector-css3']});
}, 'gallery-2010.02.10-01' ,{requires:['get','oop']});
2 changes: 1 addition & 1 deletion build/gallery-jsonp/gallery-jsonp-min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4556a62

Please sign in to comment.