Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Enhance withImageSize utility function so it queues up requests for t…

…he same image's dimensions if the image has not yet been loaded; this prevents creating a new Image object for each of those requests.
  • Loading branch information...
commit 58d2df75489ba9ec09b9a4d7299864e07f48f84a 1 parent ad97e7a
Jason Johnston authored
Showing with 13 additions and 3 deletions.
  1. +13 −3 sources/Util.js
View
16 sources/Util.js
@@ -67,14 +67,24 @@
* @param {Object} ctx A context object which will be used as the 'this' value within the executed callback function
*/
withImageSize: function( src, func, ctx ) {
- var size = imageSizes[ src ], img;
+ var size = imageSizes[ src ], img, queue;
if( size ) {
- func.call( ctx, size );
+ // If we have a queue, add to it
+ if( Object.prototype.toString.call( size ) === '[object Array]' ) {
+ size.push( [ func, ctx ] );
+ }
+ // Already have the size cached, call func right away
+ else {
+ func.call( ctx, size );
+ }
} else {
+ queue = imageSizes[ src ] = [ [ func, ctx ] ]; //create queue
img = new Image();
img.onload = function() {
size = imageSizes[ src ] = { w: img.width, h: img.height };
- func.call( ctx, size );
+ for( var i = 0, len = queue.length; i < len; i++ ) {
+ queue[ i ][ 0 ].call( queue[ i ][ 1 ], size );
+ }
img.onload = null;
};
img.src = src;
Please sign in to comment.
Something went wrong with that request. Please try again.