From: rwldrn Date: Tue, 15 Feb 2011 22:54:51 +0000 (-0500) Subject: [#312] Clean Popcorn.getScript() X-Git-Tag: v0.4~48^2~2 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=36ac9f0c5b0284c6568b042853fdb1dd720faf38;p=popcorn-js.git [#312] Clean Popcorn.getScript() --- diff --git a/popcorn.js b/popcorn.js index 9f63c0c..8c87f9c 100644 --- a/popcorn.js +++ b/popcorn.js @@ -884,12 +884,15 @@ }; Popcorn.xhr = function ( options ) { - - if ( options.dataType && options.dataType.toLowerCase() === "jsonp" ) { + + if ( options.dataType && + ( options.dataType.toLowerCase() === "jsonp" || + options.dataType.toLowerCase() === "script" ) ) { Popcorn.xhr.getJSONP( options.url, - options.success + options.success, + options.dataType.toLowerCase() === "script" ); return; } @@ -958,7 +961,23 @@ - Popcorn.xhr.getJSONP = function ( url, success ) { + Popcorn.xhr.getJSONP = function ( url, success, isScript ) { + + // If this is a script request, ensure that we do not call something that has already been loaded + if ( isScript ) { + + var scripts = document.querySelectorAll('script[src="' + url + '"]'); + + // If there are scripts with this url loaded, early return + if ( scripts.length ) { + + // Execute success callback and pass "exists" flag + success && success( true ); + + return; + } + } + var head = document.getElementsByTagName("head")[0] || document.documentElement, script = document.createElement("script"), @@ -966,44 +985,66 @@ fired = false, params = [], callback; - - if ( paramStr ) { + + + if ( paramStr && !isScript ) { params = paramStr.split("&"); } - callback = params.length ? params[ params.length - 1 ].split("=")[1] : "jsonp"; - - - if ( !paramStr ) { + + if ( !paramStr && !isScript ) { url += "?callback=" + callback; } script.src = url; - if ( callback ) { + if ( callback && !isScript ) { // define the jsonp success callback globally window[ callback ] = function ( data ) { - success( data ); + + success && success( data ); fired = true; + }; } script.onload = script.onreadystatechange = function() { - if ( fired || ( this.readyState === "loaded" || this.readyState === "complete") ) { + // Executing remote scripts + if ( isScript && ( !script.readyState || /loaded|complete/.test( script.readyState ) ) ) { + + success && success(); + + } + + // Executing for JSONP requests + if ( fired || /loaded|complete/.test( script.readyState ) ) { // cleanup in here delete window[ callback ]; + head.removeChild( script ); } + }; head.insertBefore( script, head.firstChild ); + + return; + + }; + Popcorn.getJSONP = Popcorn.xhr.getJSONP; + + Popcorn.getScript = Popcorn.xhr.getScript = function( url, success ) { + + return Popcorn.xhr.getJSONP( url, success, true ); + }; + // Exposes Popcorn to global context global.Popcorn = Popcorn; diff --git a/test/data/remoteA.js b/test/data/remoteA.js new file mode 100644 index 0000000..aff37a3 --- /dev/null +++ b/test/data/remoteA.js @@ -0,0 +1 @@ +Popcorn.AlphaLib = function() {}; \ No newline at end of file diff --git a/test/data/remoteB.js b/test/data/remoteB.js new file mode 100644 index 0000000..3c96ce2 --- /dev/null +++ b/test/data/remoteB.js @@ -0,0 +1 @@ +Popcorn.BetaLib = function() {}; \ No newline at end of file diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index c1660c1..55df750 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -1064,6 +1064,9 @@ test("Index Integrity", function () { + + + module("Popcorn XHR"); test("Basic", function () { @@ -1255,8 +1258,8 @@ test("JSONP Response", function () { ); - Popcorn.xhr.getJSONP( - 'data/jsonp.json', + Popcorn.getJSONP( + 'data/jsonp.json?callback=jsonp', function( data ) { @@ -1272,6 +1275,107 @@ test("JSONP Response", function () { ); }); +test("Popcorn.getScript()", function () { + + var expects = 8, + count = 0; + + function plus() { + if ( ++count === expects ) { + start(); + } + } + + expect(expects); + + stop(); + + + Popcorn.xhr({ + + url: "data/remoteA.js", + + dataType: "script", + + success: function() { + + ok( true, "getScript A returned"); + plus(); + + + + ok( Popcorn.AlphaLib, "Popcorn.xhr.getScript remoteA.js loaded: `Popcorn.AlphaLib` is available"); + plus(); + + } + }); + + Popcorn.getScript( + + "data/remoteB.js", + + function() { + + ok( true, "getScript B returned"); + plus(); + + + ok( Popcorn.BetaLib , "Popcorn.getScript remoteB.js loaded: `Popcorn.BetaLib` is available "); + plus(); + + } + ); + + + Popcorn.getScript( + + "http://processingjs.org/content/download/processing-js-1.0.0/processing-1.0.0.min.js", + + function() { + + ok( true, "getScript C returned"); + plus(); + + + ok( ("Processing" in window) , "Popcorn.getScript http://processingjs.org/content/download/processing-js-1.0.0/processing-1.0.0.min.js loaded: `Processing` is available"); + plus(); + + + delete window["Processing"]; + } + ); + + + + Popcorn.xhr({ + + url: "data/remoteA.js", + + dataType: "script", + + success: function( exists ) { + + ok( exists, "Success, remoteA loaded once"); + plus() + } + }); + + Popcorn.getScript( + + "data/remoteB.js", + + function( exists ) { + + ok( exists, "Success, remoteB loaded once"); + plus() + + } + ); + + +}); + + test("XML Response", function () { var expects = 2, @@ -1344,6 +1448,7 @@ test("dataType: XML Response", function () { }); + module("Popcorn Parser"); test("Parsing Functions", function () {