};
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;
}
- 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"),
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;
+
+
+
module("Popcorn XHR");
test("Basic", function () {
);
- Popcorn.xhr.getJSONP(
- 'data/jsonp.json',
+ Popcorn.getJSONP(
+ 'data/jsonp.json?callback=jsonp',
function( data ) {
);
});
+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,
});
+
module("Popcorn Parser");
test("Parsing Functions", function () {