From 9a0dc6a136d1ff51df79a48ea8eb546a4245b7f5 Mon Sep 17 00:00:00 2001 From: rwldrn Date: Mon, 20 Dec 2010 13:28:52 -0500 Subject: [PATCH] [#176] handle function calls after dom ready --- popcorn.js | 28 +++++++++++++++++++++------- test/popcorn.unit.js | 15 ++++++++++++++- 2 files changed, 35 insertions(+), 8 deletions(-) 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) {}; }); -- 2.43.0