Ticket #411 (new enhancement)
Asset.images - Handle image load errors
| Reported by: | kuralj | Owned by: | tomocchino |
|---|---|---|---|
| Type: | enhancement | Priority: | minor |
| Milestone: | Mootools version 1.3 | Component: | Plugins |
| Keywords: | Cc: |
Description
Asset.images will not fire onComplete if there is an issue with any of the images passed to it (eg. server issue, missing file, incorrect filename).
Any associated progress bar will also not display correctly because onProgress is not called for image errors.
The issue occurs because counter++ and onProgress is only called for image.onload. Adding an onerror function that duplicates the onload functionality would solve this.
Case scenario: If you wanted to preload images for a UI, then init the UI when the images are loaded, a single image error would mean the UI would never display.
Sample fix:
images: function(sources, options){
options = $merge({
onComplete: $empty,
onProgress: $empty
}, options);
if (!sources.push) sources = [sources];
var images = [];
var counter = 0;
sources.each(function(source){
var img = new Asset.image(source, {
'onload': function(){
options.onProgress.call(this, counter, sources.indexOf(source));
counter++;
if (counter == sources.length) options.onComplete();
},
'onerror': function(){
// could pass a flag to onProgress indicating that the image failed
options.onProgress.call(this, counter, sources.indexOf(source));
counter++;
if (counter == sources.length) options.onComplete();
}
});
images.push(img);
});
return new Elements(images);
}