]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#176] handle function calls after dom ready
authorrwldrn <waldron.rick@gmail.com>
Mon, 20 Dec 2010 18:28:52 +0000 (13:28 -0500)
committerrwldrn <waldron.rick@gmail.com>
Mon, 20 Dec 2010 18:28:52 +0000 (13:28 -0500)
popcorn.js
test/popcorn.unit.js

index 319fdd90eeb88e3e3452a05e71fa38a2b913db9a..9164c260702a535c735fa615428617b6e15f29bb 100644 (file)
@@ -11,7 +11,8 @@
   
   // ready fn cache
   readyStack = [], 
-  readyBound = false, 
+  readyBound = false,
+  readyFired = false,
   
 
   //  Declare a pseudo-private constructor
       //  Originally proposed by Daniel Brooks
       
       if ( typeof entity === "function" ) {
+      
+        //  If document ready has already fired
+        if ( document.readyState === "interactive" || document.readyState === "complete" ) {
+          
+          entity(document, Popcorn);
+          
+          return;
+        }
         
-        readyStack.push( entity );
         
+        readyStack.push( entity );
+
         //  This process should happen once per page load
         if ( !readyBound ) {
 
           //  set readyBound flag
           readyBound = true;
-          
+
           var DOMContentLoaded  = function () {
             
+            readyFired = true;
+            
             //  remove this listener
             document.removeEventListener( "DOMContentLoaded", DOMContentLoaded, false );
-            
+
             //  Execute all ready function in the stack
             for ( var i = 0; i < readyStack.length; i++ ) {
-            
+
               readyStack[i].call( document, Popcorn );
-            
+
             }
             //  GC readyStack
             readyStack = null;  
           };
-          
+
           document.addEventListener( "DOMContentLoaded", DOMContentLoaded, false);
         }
+
+        
         
         return;  
       }
index 6ea97f7e0bf5313137fe0ab5023a01826c6756ba..3399a6c510b449f93dd42b84edd92fe35950a7e8 100644 (file)
@@ -2,7 +2,7 @@
 module("Popcorn");
 test("API", function () {
   
-  var expects = 3, 
+  var expects = 4, 
       count = 0;
   
   expect(expects);
@@ -33,6 +33,19 @@ test("API", function () {
     plus();
     
   } catch (e) {};
+  
+  
+  try {  
+  
+    Popcorn(function() { 
+    
+      ok(1, "Popcorn calls its function argument"); 
+      plus();
+      
+      
+    });
+    
+  } catch (e) {};  
 
   
 });