From: rwldrn Date: Mon, 20 Dec 2010 15:44:54 +0000 (-0500) Subject: [#176] Add DOMContentLoaded check to popcorn core X-Git-Tag: v0.2~34^2~1^2~3 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=76b9c532a4d9d16ac6d22c8b2c4aa5fdff1a04a6;p=popcorn-js.git [#176] Add DOMContentLoaded check to popcorn core --- diff --git a/popcorn.js b/popcorn.js index 73b84a2..0bb56eb 100644 --- a/popcorn.js +++ b/popcorn.js @@ -11,9 +11,14 @@ // ID string matching rIdExp = /^(#([\w\-\_\.]+))$/, + + // ready fn cache + readyStack = [], + readyBound = false, + // Declare a pseudo-private constructor - // This constructor returns the instance object. + // Returns an instance object. Popcorn = function( entity ) { // Return new Popcorn object return new Popcorn.p.init( entity ); @@ -27,13 +32,40 @@ var elem, matches; + // Supports Popcorn(function () { /../ }) + // Originally proposed by Daniel Brooks if ( typeof entity === "function" ) { - document.addEventListener( "DOMContentLoaded", entity, false); + readyStack.push( entity ); + + // This process should happen once per page load + if ( !readyBound ) { + + // set readyBound flag + readyBound = true; + + var DOMContentLoaded = function () { + + // 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; } + matches = rIdExp.exec( entity ); @@ -41,6 +73,7 @@ elem = document.getElementById(matches[2]); } + this.video = elem ? elem : null; this.data = { @@ -208,6 +241,7 @@ var methods = "load play pause currentTime playbackRate mute volume duration", ret = {}; + // Build methods, store in object that is returned and passed to extend Popcorn.forEach( methods.split(/\s+/g), function( name ) { @@ -728,7 +762,7 @@ - - global.Popcorn = Popcorn; + // Exposes Popcorn and a shorthand to global context + global.Popcorn = global.$P = Popcorn; })(window, document); diff --git a/test/ready.html b/test/ready.html new file mode 100644 index 0000000..3b835a6 --- /dev/null +++ b/test/ready.html @@ -0,0 +1,49 @@ + + + + Popcorn API + + + + + + + + +