in partnership with mediatemple

Ticket #411 (new enhancement)

Opened 9 months ago

Last modified 2 days ago

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);
}

Change History

Changed 8 months ago by ibolmo

  • owner set to tomocchino
  • milestone changed from Mootools version 1.2 to Mootools version 1.3

Changed 2 months ago by lgladdy

  • component set to Core

This should probably be added to the 'Notes' section of the 1.2 documentation - I spent a couple of hours trying to figure out why it wasn't firing!

Changed 2 months ago by lgladdy

  • component changed from Core to Plugins

Ops, adding a comment set component to core - changing to plugins.

Changed 2 days ago by tomocchino

  • milestone set to Mootools version 1.3
Note: See TracTickets for help on using tickets.