From f3a078642083b66644b38428bce22f4f3ba52c19 Mon Sep 17 00:00:00 2001 From: rwldrn Date: Tue, 18 Jan 2011 15:05:51 -0500 Subject: [PATCH] [#268] Add JSONP support to Popcorn.xhr --- popcorn.js | 69 ++++++++++++++++++++++++++++++++++++++++---- test/data/jsonp.json | 1 + test/popcorn.unit.js | 54 ++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 test/data/jsonp.json diff --git a/popcorn.js b/popcorn.js index 83ef378..7a45ce7 100644 --- a/popcorn.js +++ b/popcorn.js @@ -792,9 +792,12 @@ return parser; }; - - - var setup = { + + + // Cache references to reused RegExps + var rparams = /\?/, + // XHR Setup object + setup = { url: '', data: '', dataType: '', @@ -807,15 +810,34 @@ }; Popcorn.xhr = function ( options ) { - + + if ( ( options.dataType || "" ).toLowerCase() === "jsonp" ) { + + Popcorn.xhr.getJSONP( + options.url, + options.success + ); + return; + } + var settings = Popcorn.extend( {}, setup, options ); settings.ajax = settings.xhr(); if ( settings.ajax ) { + + if ( settings.type === "GET" && settings.data ) { + + // append query string + settings.url += ( rparams.test( settings.url ) ? "&" : "?") + settings.data; + + // Garbage collect and reset settings.data + settings.data = null; + } + settings.ajax.open( settings.type, settings.url, settings.async ); - settings.ajax.send( null ); + settings.ajax.send( settings.data = null ? null : settings.data ); return Popcorn.xhr.httpData( settings ); } @@ -851,6 +873,43 @@ + Popcorn.xhr.getJSONP = function ( url, success ) { + + var head = document.getElementsByTagName("head")[0] || document.documentElement, + script = document.createElement("script"), + paramStr = url.split("?")[1], + fired = false, + params, callback; + + script.src = url; + + params = paramStr.split("&"); + + callback = params.length ? params[ params.length - 1 ].split("=")[1] : Popcorn.guid("jsonp"); + + if ( callback ) { + // define the jsonp success callback globally + window[ callback ] = function ( data ) { + success( data ); + fired = true; + }; + } + + script.onload = script.onreadystatechange = function() { + + if ( fired || ( this.readyState === "loaded" || this.readyState === "complete") ) { + + // cleanup in here + delete window[ callback ]; + head.removeChild( script ); + } + }; + + head.insertBefore( script, head.firstChild ); + + } + + // Exposes Popcorn to global context global.Popcorn = Popcorn; diff --git a/test/data/jsonp.json b/test/data/jsonp.json new file mode 100644 index 0000000..eb82a23 --- /dev/null +++ b/test/data/jsonp.json @@ -0,0 +1 @@ +jsonp({ "data": {"lang": "en", "length": 25} }); \ No newline at end of file diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index a1f131e..fcae5c8 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -1071,6 +1071,60 @@ test("JSON Response", function () { }); +test("JSONP Response", function () { + + var expects = 4, + count = 0; + + function plus() { + if ( ++count === expects ) { + start(); + } + } + + expect(expects); + + stop( 10000 ); + + + var testObj = { "data": {"lang": "en", "length": 25} }; + + Popcorn.xhr({ + + url: 'data/jsonp.json?callback=jsonp', + dataType: 'jsonp', + success: function( data ) { + + ok(data, "xhr returns data"); + plus(); + + + console.log(data); + ok( QUnit.equiv(data, testObj) , "Popcorn.xhr({}) data.json returns an object of data"); + plus(); + + } + }); + + Popcorn.xhr.getJSONP( + 'data/jsonp.json?callback=jsonp', + + function( data ) { + + ok(data, "xhr returns data"); + plus(); + + + console.log(data); + ok( QUnit.equiv(data, testObj) , "Popcorn.xhr.getJSONP data.json returns an object of data"); + plus(); + + } + ); + + +}); + test("XML Response", function () { var expects = 2, -- 2.43.0