I have a function which loops and for each iteration dispatches an
event.  The event handler is inside a new class (AddSourceCommand). 
Inside the event handler class I am dispatching another event which is
being handled by a function inside the same class.  There's a variable
that originates from the initial function, is passed to the first event
handler via the event, and then passed to the second event handler via
the second event.  The variable changes each time it is sent to the
first event.

The first event handler is receiving the variable correctly.  (It
changes each time.)  But for some reason the second event handler always
receives the last variable sent, every time.

How do I get the second event handler to receive the correct variable?

See code below.

It's almost as if the configureSource handler is called every time by
the last instantiation of the AddSourceCommand class.  But I thought by
creating a new AddSourceCommand instantiation, like I did below, should
take care of that.

ImportAllFlickrSetsCommand.as
------------------
public function ImportAllFlickrSetsCommand()
{
     this.addEventListener(AddSourceEvent.ADD_FLICKR_SET,(new
AddSourceCommand()).execute);
}

public function execute(event:ImportAllFlickrSetsEvent):void {
     for each(var phSet:PhotoSet in data.photoSets) {
        newAlbum = stuffToGetNewAlbum();
        dispatchEvent(new AddSourceEvent(AddSourceEvent.ADD_FLICKR_SET,
newAlbum, phSet.title, phSet.id));
     }
     // the above 'for each' will execute 4 times.
     // the newAlbum variable sends the album that is unpacked in the
AddSourceCommand below.
     // newAlbum.name is "album1", "album2", "album3", "album4" in the
1st, 2nd, 3rd, and 4th 'for each' iteration
}


AddSourceCommand.as
------------------------
import com.adobe.webapis.flickr.FlickrService;
import com.adobe.webapis.flickr.Photo;
import com.adobe.webapis.flickr.events.FlickrResultEvent;

private var flickr:FlickrService = new FlickrService(data.API_KEY);

private var albumForSource:Album;

public function AddSourceCommand()
{
     flickr.addEventListener(FlickrResultEvent.PHOTOSETS_GET_PHOTOS,
configureSource);
}

public function execute(event:AddSourceEvent):void {
     albumForSource = event.album;
     trace("albumForSource from execute: " + albumForSource.name);
     flickr.photosets.getPhotos(event.id);  // retrieves photos from
flickr apr.  dispatches FlickrResultEvent when done.
}

private function configureSource(event:FlickrResultEvent):void {
     trace("albumForSource from configureSource: " +
albumForSource.name);
}


OUTPUT
--------------------
albumForSource from execute: album1
albumForSource from execute: album2
albumForSource from execute: album3
albumForSource from execute: album4
albumForSource from configureSource: album4
albumForSource from configureSource: album4
albumForSource from configureSource: album4
albumForSource from configureSource: album4

DESIRED OUTPUT
--------------------
albumForSource from execute: album1
albumForSource from execute: album2
albumForSource from execute: album3
albumForSource from execute: album4
albumForSource from configureSource: album1
albumForSource from configureSource: album2
albumForSource from configureSource: album3
albumForSource from configureSource: album4

ascii

Reply via email to