Take a look at some of the classes in the mx.events.* package. Generally,
your custom events will have static constants to enumerate the possible
values for 'type', i.e.
public static const ADD_RECORD:String = "addRecordEvent";
public static const DELETE_RECORD:String = "deleteRecordEvent";

To attach data to an event, for example the record to delete, you would have
a property on the event.

public var record:RecordVO;

Then, in your button's click handler, you would create a new instance of
your custom event, attach the record you want to act on to it, and dispatch
it. If you want to keep your components more loosely coupled (and you
should) you would have your buttons announce an event simply stating "Delete
the currently selected record" and have your business logic located
elsewhere that would figure out what record to delete (ex. the currently
selected item in the datagrid.)

For a simple app, and since you're just getting started, you could place the
listeners in your top-level Application MXML file. Have it subscribe to the
DELETE_RECORD etc. events on the buttons, and act accordingly. Again, when
you get further along, you'll want to move this business logic elsewhere,
but I'll leave that up to you :)

HTH,
 - Ken

On Tue, Mar 24, 2009 at 9:23 AM, secrit.service
<[email protected]>wrote:

>   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