A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Flex: can't access property of custom event

  1. #1
    Senior Member
    Join Date
    Mar 2001
    Location
    Breda, The Netherlands
    Posts
    522

    Flex: can't access property of custom event

    Hi all,

    It's me again I've changed my strategy for this problem: http://board.flashkit.com/board/show...09#post4305709
    I now want to try to update my component using a custom event. So the main problem still is: in my application I load 1 data file in the main mxml. After it loads some processing is done on it and the data is stored in an ArrayCollection. This collection holds data for three years. I want to visualise this year data by letting users click on a button labelled with that year. I call an eventhandler in the main mxml file that updates a variable named currentYear and passes this value on to a variable called year (in my new approach using a custom event, view code below) in an ItemRenderer which should then display the data from this specific year.
    Now I dispatch a custom event in the main mxml like this when a button of a certain year is clicked:
    PHP Code:
    protected function btn01ClickHandler(event:MouseEvent):void{
                    
    currentYear "2001";
                    var 
    myEvent:ChangeYearEvent = new ChangeYearEvent(currentYear);
                    
    this.dispatchEvent(myEvent);
                } 
    The custom event that passes on the selected year value is called ChangeYearEvent looks like this (pretty basic):
    PHP Code:
    package events
    {
        
    import flash.events.Event;
        
        public class 
    ChangeYearEvent extends Event
        
    {
            public var 
    year:String;
            public static const 
    CHANGE_YEAR:String "change_year";
            
            public function 
    ChangeYearEvent(year:String)
            {
                
    super(type);
                
    this.year year;
            }
            
            
    override public function clone():Event {
                return new 
    ChangeYearEvent(year);
            }
        }

    In my ItemRenderer which has to work with the data from a specific year I try to access the data like this:
    PHP Code:
    <s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                    
    xmlns:s="library://ns.adobe.com/flex/spark" 
                    
    xmlns:mx="library://ns.adobe.com/flex/mx" 
                    
    autoDrawBackground="true" creationComplete="init()">
    import mx.collections.ArrayCollection;
    private function 
    init():void{
                    
    systemManager.addEventListener(ChangeYearEvent.CHANGE_YEARsetYeartrue);
    }
    private function 
    setYear(evt:Event){

                    
    trace("year:"+evt.year);
    // and some other stuff, this is just to show the approach
                
    }
    </
    s:ItemRenderer
    The problem is that evt.year isn't found: access to an undefined property... Why? What am I missing here? The event is coming in but I can't access the property of the event. Any help is greatly appreciated. TIA.
    // Kind regards, Danielle.
    // specs: Flash CS5.5 | Flash Builder 4.6 | win xp pro

  2. #2
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    your function signature is expecting an Event object but .year is a property of ChangeYearEvent.
    just change the parameter
    Code:
    private function setYear(evt:ChangeYearEvent):void{ 
      trace("year:"+evt.year); 
      // and some other stuff, this is just to show the approach 
    }

  3. #3
    Senior Member
    Join Date
    Mar 2001
    Location
    Breda, The Netherlands
    Posts
    522
    Hi JAQAUN,
    Thanks for that, of course that's it. I did also have to add metadata to the ItemRenderer like this:
    PHP Code:
    <fx:Metadata>
            [
    Event(name="change_year"type="events.ChangeYearEvent")]
        </
    fx:Metadata
    To actually be able to read the data passed with the event. Now it works. Thanks again for all the help.
    // Kind regards, Danielle.
    // specs: Flash CS5.5 | Flash Builder 4.6 | win xp pro

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center