Understand that "custom event" has two different meanings.  You can create a
true custom event, a class of your own that extends Event, as you have
posted.  You would do that if you need to package up some data in the event
object itself.

 

But you can also apply a custom type name to a standard Event object:

dispatchEvent(new Event("myCustomTypeName"));

 

myCustomComp.addEventListener("myCustomTypeName", myCustomTypeHandler)

 

If you need data in the event handler, you can access any public property in
the dispatching object by using the reference returned by target or
currentTarget.  This second approach is much simpler that creating a custom
event class.

Private function myCustomTypeHandler(event:Event):void

 Var someData:String = event.currentTarget.myPublicProperty;

.

 

Tracy Spratt,

Lariat Services, development services available

  _____  

From: [email protected] [mailto:[email protected]] On
Behalf Of secrit.service
Sent: Tuesday, March 24, 2009 9:24 AM
To: [email protected]
Subject: [flexcoders] Custom Classes and Custom Events

 

Hi all,

The last couple of weeks I wrote my first application in Flex. Today I want
to refactor it, meaning : using custom classes and custom events.

I have following :

I created an application with a couple of buttons and with a custom
component (myCustomComp)  in it. This custom component is based on a Vbox an
contains :
     - an HBox (mySearchbar) with a textfield and button
     - a datagrid which contains data from a database
     - an HBox (myToolbar) with a button and a label 

The buttons are used for adding, modifying and deleteing records of the
database. SO I was thinking of creating a custom event (myCustomEvent).

The code I have so far is following :

package events {
 
 import flash.events.Event;
 
 public class myCustomEvent extends Event {
   
            public function SpellEvent(type:String) {
                         super(type);
            }
  
           override public function clone():Event{
                        var evt:myCustomEvent = new myCustomEvent(type);
                        return evt;
            }
  
 }
 
}

I was thinking of adding event listners to my buttons. For example my button
to add a new record will have a addRecordEventListener. In this listener I
create a new instance of myCustEvent and pass my new record as a parameter.
The same I will do with my button to delete a record.

My question is where and how I should make the difference between the 2
operations.
I suppose I have to create a new instance of my event and besides my new
record I also need to pass the type of operation : something like new
myCustomEvent(newRecord, "addRecord") or new myCustomEvent(newRecord,
"deleteRecord"). But in this case, how do I process the different
operations?

I'm lost and hoping for a clear answer.

Thanks



Reply via email to