views:

26

answers:

1

Hi,

I'm trying to learn how to use custom events in Flex.

I'm following Oliver Merk's tutorial found here: blog

The custom event works if I implement it using MXML in the main app. But, if I use actionscript, then I get error 1119: Access of possibly undefined property ADD_PRODUCT through a reference with static type Class.

My Event: In the events subdirectory, I've got:

package events {

   import flash.events.Event;

   public class AddProductEvent extends Event {

      public var productName:String;

      public function AddProductEvent( type:String, productName:String ) {

         super( type );
         this.productName = productName;

      }

      override public function clone():Event {
         return new AddProductEvent( type, productName );
      }

   }
}

In the component, I've got a radioButtonGroup

<mx:RadioButtonGroup id="choicesRadioButtonGroup" itemClick="onButtonClick()"/>



private function onButtonClick():void {
            var myEventObj:Event = new AddProductEvent("addProduct", "Flex T-shirt");
            dispatchEvent(myEventObj);
        } 

This is the metadata in the component and the import statement:

<mx:Metadata>
    [Event (name="addProduct", type="events.AddProductEvent")]
</mx:Metadata>

import events.AddProductEvent;

In the main app, I've got:

import events.AddProductEvent;

private function onAddProduct( event:AddProductEvent ):void {
mx.controls.Alert.show('Attached data was ' + event.productName);
}

If I implement the component in the main app like this:

<visualcomponent:PopWindow addProduct="onAddProduct(event)" />

then everything works.

If I implement the component in the main app in actionscript like this, then I get an error:

public function clickHandler2(event:MouseEvent):void {

    if(event.currentTarget.selected){popWindow = new PopWindow;
        queryBuilder(event.currentTarget);
        PopUpManager.addPopUp(popWindow, my_view, false);
        PopUpManager.centerPopUp(popWindow);

            popWindow.addEventListener(AddProductEvent.ADD_PRODUCT, onAddProduct);}

    }

I get the error on the addEventListener line. What am I doing wrong? Any advice?

Thank you.

-Laxmidi

+2  A: 

Your AddProductEvent class doesn't seem to expose a public static string called ADD_PRODUCT which has the value "addProduct" which is what it looks like you are trying to do.

Gregor Kiddie
Hi Gregor Kiddie, Thank you for your message. I'm still learning about custom events. I don't understand what I need to change. I'm trying to have my event send the productName string back to the main app. Any suggestions? Thank you.
Laxmidi
Hi Gregor, I added public static const ADD_PRODUCT:String = "addProduct"; in the event after public class AddProductEvent extends Event {... and now it works. Thank you!
Laxmidi