Search code examples
apache-flexflex3dispatchevent

Getting results from dispatchEvent in Flex 3


I have the following custom event:

package com.un.photoManager.events
{
import flash.events.Event;

import mx.collections.ArrayCollection;


public class CreateAlbumFolderEvent extends Event
{
    // when creating an album, we need to know which albumfolder to put it in, 0 is the default group;
    public var albumFolderID:int = 0;
    public var name:String;

    public function CreateAlbumFolderEvent(type:String, name:String, albumFolderID:int = 0, cancelable:Boolean = false)
    {
        super(type, true, cancelable);
        this.name = name;
        this.albumFolderID = albumFolderID;

    }

}
}

The event gets called from a popup using the following code:

protected function handleCreate():void
        {
            var event:CreateAlbumFolderEvent;
            var selectedItemType:String;

            if (folderAlbum == CREATE_ALBUM)
            {
                event = new CreateAlbumFolderEvent(EventConstants.CREATE_ALBUM, newAlbumFolder.text, selectedAlbumFolderID);
                selectedItemType = "Album";
            }
            else
            {
                event = new CreateAlbumFolderEvent(EventConstants.CREATE_ALBUM_FOLDER, newAlbumFolder.text);
                selectedItemType = "Folder";
            }
            dispatchEvent(event);
            FolderBrowse.lastSelectedItemType = selectedItemType;
            PopUpManager.removePopUp(this);
        }

What I am trying to do is to capture the response that is handed back. Here is a screenshot of Charles showing the response. The Result value is what I am looking to be able to use once the dispatchEvent(event); has executed.

Screenshot from Charles

I have been working on this for several hours looking at blog & forum posts and have not been able to get a solution to work. Ideally, code samples would be nice, but right now any help would be appreciated.


Solution

  • You are required to override the Event.clone() method in your subclass. The clone() method returns a cloned copy of the event object by setting the type property and any new properties in the clone. Typically, you define the clone() method to return an event instance created with the new operator.

    package sample
    {
        import flash.events.Event;
        import mx.collections.ArrayCollection;
    
        public class CustomEvent extends Event
        {
    
            public static var DATA_LOADED:String = "dataLoaded";
    
            private var _data:ArrayCollection;
    
            public function CustomEvent(type:String, bubbles:Boolean=false, cancelable:Boolean=false, data:ArrayCollection=null)
            {
                super(type, bubbles, cancelable);
                _data = data;
            }
    
            override public function clone():Event
            {
                return new CustomEvent(type, bubbles, cancelable, data);
            }
    
            public function get data():ArrayCollection
            {
                return _data;
            }
        }
    }