From: rwldrn Date: Mon, 20 Dec 2010 18:28:52 +0000 (-0500) Subject: [#176] handle function calls after dom ready X-Git-Tag: v0.2~34^2~1^2 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=9a0dc6a136d;p=popcorn-js.git [#176] handle function calls after dom ready --- diff --git a/popcorn.js b/popcorn.js index 319fdd9..9164c26 100644 --- a/popcorn.js +++ b/popcorn.js @@ -11,7 +11,8 @@ // ready fn cache readyStack = [], - readyBound = false, + readyBound = false, + readyFired = false, // Declare a pseudo-private constructor @@ -33,32 +34,45 @@ // 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; } diff --git a/test/popcorn.unit.js b/test/popcorn.unit.js index 6ea97f7..3399a6c 100644 --- a/test/popcorn.unit.js +++ b/test/popcorn.unit.js @@ -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) {}; });