]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#312] Clean Popcorn.getScript()
authorrwldrn <waldron.rick@gmail.com>
Tue, 15 Feb 2011 22:54:51 +0000 (17:54 -0500)
committerrwldrn <waldron.rick@gmail.com>
Tue, 15 Feb 2011 22:54:51 +0000 (17:54 -0500)
popcorn.js
test/data/remoteA.js [new file with mode: 0644]
test/data/remoteB.js [new file with mode: 0644]
test/popcorn.unit.js

index 9f63c0c3967a259a0bec1bc8adda073887f1460a..8c87f9c1304031a3ac6142b0d225a1b5cde530c7 100644 (file)
   };   
   
   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;
diff --git a/test/data/remoteA.js b/test/data/remoteA.js
new file mode 100644 (file)
index 0000000..aff37a3
--- /dev/null
@@ -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 (file)
index 0000000..3c96ce2
--- /dev/null
@@ -0,0 +1 @@
+Popcorn.BetaLib = function() {};
\ No newline at end of file
index c1660c1514f50fc088742482096ddbaff8143ddb..55df750462a19f3513ac68248435a3e817b9c3b6 100644 (file)
@@ -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 () {