This project is archived and is in readonly mode.

#16 ✓duplicate
Thomas Aylott

Asset.images - Handle image load errors

Reported by Thomas Aylott | May 28th, 2008 @ 10:00 AM

Originally posted on Trac by kuralj

Original Trac Ticket

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

Trac Attachments

Trac Comments

Changed 9 months ago by ibolmo



                  Changed 9 months ago by ibolmo

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

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

Changed 3 months ago by lgladdy


        
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 3 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! 

          component
              set to Core
        
      

Changed 3 months ago by lgladdy


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

                  Changed 3 months ago by lgladdy

          component
              changed from Core to Plugins

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

          component
              changed from Core to Plugins
        
      

Changed 2 weeks ago by tomocchino



                  Changed 2 weeks ago by tomocchino

          milestone
              set to Mootools version 1.3

          milestone
              set to Mootools version 1.3
        
      

Comments and changes to this ticket

  • Sylvain

    Sylvain June 23rd, 2008 @ 11:50 AM

    Hi !

    i tried this code, it's ok on IE6 and FF2 but not on IE7...

    -------------------------------------------------------------

    if($$('.imgrateprice').length > 0){

    $$('.imgrateprice').each(function(img){

    var aImg = new Asset.image(img.getProperty('src'),{

    'onerror': function(){

    /*a function to hide image*/

    }.bind(this)

    });

    },this);

    }

    -------------------------------------------------------------

    have you a solution or an idea ?

    thx

  • fakedarren

    fakedarren February 8th, 2010 @ 02:36 PM

    • State changed from “new” to “open”
    • Tag set to enhancement
    • Assigned user changed from “Valerio” to “fakedarren”
    • Milestone cleared.
  • fakedarren

    fakedarren February 13th, 2010 @ 11:32 AM

    • State changed from “open” to “duplicate”

    I am marking this as a duplicate of #177. Please watch #177 if you wish to receive status updates.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

Referenced by

Pages