1 Reply Latest reply on Nov 5, 2007 10:36 AM by ntsiii

    capture event from mxml component

    vtxr1300 Level 1
      I have an accordian control in my main mxml application. Each item in the control is a custom mxml component that I created that consists of a label and some text. When the label is clicked, I need to fire off a message to the containing application with a string value. I'm not sure how to do this. Here's what I have right now. I'm not sure if I'm going down the right track but if I am, how do I pass the string argument with the event and then capture this event and the argument in the main application?

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="200">
      private var _title:String = "";
      private var _desc:String = "";


      public function set Title(title:String):void{
      _title = title;

      public function set Description(desc:String):void{
      _desc = desc;

      private function SetMovie(url:String):void{
      dispatchEvent(new Event("MovieTitle"));
      <mx:Label id="lblTitle" text="{_title}" width="190" click="SetMovie('testmovie.flv');/>
      <mx:Text id="txtDescription" text="{_desc}" width="190"/>
        • 1. Re: capture event from mxml component
          ntsiii Level 3
          You have several issues, and several options here. First, a custom event can pass any data you want, and is not very hard to create.

          However, there is a still easier way. All of the Event objects have a "target" and "currentTarget" property which give you a reference to the object that dispatched the event.

          So, in your component, implement a public property, say like this:
          public function get Title():String{
          return _title;

          then in a handler function you can do:
          private function onMovieTitle(event:Event):void {
          var sMovieTitle:String = event.currentTarget.Title; //watch out for reserved words, though

          There are two ways to listen for an event. One easy way is to use a bubbling event. Some folks advise against bubbling event because of potential event name collisions, but this may not be a concern for you. It has not yet concerned me enough to make me avoid using bubbling.

          The other way is to declare handler on the component itself. Also, if you use a metadata tag, you can assign the handler on the mxml tag, instead of using addEventListener():

          <mx:Canvas xmlns:mx=" http://www.adobe.com/2006/mxml" width="200">
          [Event(name="MovieTitle", type="flash.events.Event")]

          Then, in you main app:
          <myComp id="mc1" ... MovieTitle="onMovieTitle" ...

          Without the metadata, you would do

          Using a bubbling event:
          change the dispatchEvent to this:
          dispatchEvent(new Event("MovieTitle",true)); //the 'true' makes it bubble

          Then, in the main app, listen ON the main app(this):