diff --git a/jquery.imgpreload.js b/jquery.imgpreload.js index 887a96f..4bcc1df 100644 --- a/jquery.imgpreload.js +++ b/jquery.imgpreload.js @@ -38,6 +38,7 @@ if ('undefined' != typeof jQuery) // extend jquery (because i love jQuery) $.imgpreload = function (imgs,settings) { + var deferred = $.Deferred(); settings = $.extend({},$.fn.imgpreload.defaults,(settings instanceof Function)?{all:settings}:settings); // use of typeof required @@ -67,16 +68,29 @@ if ('undefined' != typeof jQuery) $.data(img_obj, 'loaded', ('error'==e.type)?false:true); - if (settings.each instanceof Function) { settings.each.call(img_obj); } + if (settings.each instanceof Function) { + settings.each.call(img_obj); + } + + if (deferred.notifyWith instanceof Function) { // support for jQuery < 1.7 + deferred.notify(img_obj); + } // http://jsperf.com/length-in-a-variable - if (loaded.length>=imgs.length && settings.all instanceof Function) { settings.all.call(loaded); } + if (loaded.length>=imgs.length) { + if (settings.all instanceof Function) { + settings.all.call(loaded); + } + deferred.resolve(loaded); + } $(this).unbind('load error'); }); img.src = url; }); + + return deferred.promise(); }; $.fn.imgpreload = function(settings) diff --git a/jquery.imgpreload.min.js b/jquery.imgpreload.min.js index 52564af..beb70cd 100644 --- a/jquery.imgpreload.min.js +++ b/jquery.imgpreload.min.js @@ -1,3 +1 @@ -/* v1.5 */ -/* https://github.com/farinspace/jquery.imgpreload */ -if("undefined"!=typeof jQuery){(function(a){a.imgpreload=function(b,c){c=a.extend({},a.fn.imgpreload.defaults,c instanceof Function?{all:c}:c);if("string"==typeof b){b=new Array(b)}var d=new Array;a.each(b,function(e,f){var g=new Image;var h=f;var i=g;if("string"!=typeof f){h=a(f).attr("src")||a(f).css('background-image').replace(/^url\((?:"|')?(.*)(?:'|")?\)$/mg, "$1");i=f}a(g).bind("load error",function(e){d.push(i);a.data(i,"loaded","error"==e.type?false:true);if(c.each instanceof Function){c.each.call(i)}if(d.length>=b.length&&c.all instanceof Function){c.all.call(d)}a(this).unbind("load error")});g.src=h})};a.fn.imgpreload=function(b){a.imgpreload(this,b);return this};a.fn.imgpreload.defaults={each:null,all:null}})(jQuery)} +if("undefined"!=typeof jQuery){(function($){$.imgpreload=function(imgs,settings){var deferred=$.Deferred();settings=$.extend({},$.fn.imgpreload.defaults,settings instanceof Function?{all:settings}:settings);if("string"==typeof imgs){imgs=new Array(imgs)}var loaded=new Array;$.each(imgs,function(i,elem){var img=new Image;var url=elem;var img_obj=img;if("string"!=typeof elem){url=$(elem).attr("src")||$(elem).css("background-image").replace(/^url\((?:"|')?(.*)(?:'|")?\)$/gm,"$1");img_obj=elem}$(img).bind("load error",function(e){loaded.push(img_obj);$.data(img_obj,"loaded","error"==e.type?false:true);if(settings.each instanceof Function){settings.each.call(img_obj)}if(deferred.notifyWith instanceof Function){deferred.notify(img_obj)}if(loaded.length>=imgs.length){if(settings.all instanceof Function){settings.all.call(loaded)}deferred.resolve(loaded)}$(this).unbind("load error")});img.src=url});return deferred.promise()};$.fn.imgpreload=function(settings){$.imgpreload(this,settings);return this};$.fn.imgpreload.defaults={each:null,all:null}})(jQuery)} \ No newline at end of file diff --git a/tests/index.html b/tests/index.html index 014485a..a017486 100644 --- a/tests/index.html +++ b/tests/index.html @@ -194,6 +194,15 @@ $.imgpreload( the_images, all_callback ); }); + asyncTest('preload image array - promise resolved', function() + { + reset_image_urls(); + + $.imgpreload( the_images ).then(function(images) { + all_callback.call(images); + }); + }); + asyncTest('preload image array - { all: callback }', function() { reset_image_urls();