Dispatch customized events

For a project using amfphp, I am having asynchronism issues. It’s a website/photos gallery, so I get photo datas (from the database) and load the matching thumbnails. But I can’t know when image loadings will be over or which of them will be the last.

I can give a ‘reference’ from parent to child class, or listen loadings in child class from parent, but in this instance, there is a ‘cleaner’ way to do that : to create customized events.
I’ve a ‘ThumbnailsBar’ object (which contains all my thumbnails) which is in a ‘Main’ class.

In Main :

//listen event of end of loadings
myThumbnailsBar.addEventListener(ThumbnailsBar.END_LOADING, goToNext);

In ThumbnailsBar:

//nbTotalThumbnails : total number of thumbnails
//nbLoadedThumbnails : number of thumbnails already loaded
//END_LOADING : event of end of loadings

//at every thumbnail’s loading, increment of nbLoadedThumbnails and check if equal to nbTotalThumbnails
if(nbTotalThumbnails == nbLoadedThumbnails ){
//if it’s equal all loadings are over so we can dispatch the event
dispatchEvent(new Event(ThumbnailsBar.END_LOADING));

Leave a Reply

Your email address will not be published. Required fields are marked *