0

I have to work on a project someone else started, but can't contact him because he's out of the country at the moment. Anyway. There is a main mxml and a custom component called "admin".

In admin he declared an event like this:

    <fx:Metadata>
    [Event(name="sluitFrame")]
</fx:Metadata>

And in a certain function within the component, this event is called upon like this:

dispatchEvent(new Event("sluitFrame"));

This event sluitFrame closes a frame with some admin tools. I need to change the way this works so i'd like to find the corresponding code. On the main mxml there's this code:

                <comp:Admin id="compAdmin" creationPolicy="none"
                         sluitFrame="verbergAdminComponent(event)"/>

So if i understand correctly sluitFrame calls to a custom even called "verbergAdminComponent(event)". So i guess i need this event to change the way the admin frame is closed etc. But this event is nowhere to be found. So I don't understand how "verbergAdminComponent(event)" works or where I can make changes to this event.

Any help is more than welcome and very much needed :)

Kevin Verhoeven
  • 179
  • 3
  • 18

3 Answers3

2

The [Event... line simply lets the compiler and IDE know that the component/class (in this case, Admin) may dispatch an event by that name. This is important because when someone declares an instance of Admin in a MXML tag, the compiler knows that this event (in this case, sluitFrame) is a valid property. In other words, it lets the compiler know that you can set an event listener in the MXML tag. In your case, every time the Admin object dispatches a sluitFrame event, the function verbergAdminComponent is called and the sluitFrame Event is passed to it.

user1103976
  • 539
  • 3
  • 11
1

verbergAdminComponent would be the name of an event handler. It should be a method either in the mxml, in an included .as, or in a base class of that MXML.

Marc Hughes
  • 5,808
  • 3
  • 36
  • 46
  • I searched for it everywhere, but i can't seem to find it. As far as i know there isn't a way to call to a class that is saved somewhere else, is there? I really did check the entire project. Can that method have a different name as the event handler? And if so, how are they connected with each other? – Kevin Verhoeven Jun 11 '12 at 00:43
  • There is either a verbergAdminComponent method somewhere in the hierarchy, or you have compiler errors. Is it possible there is a pre-compiled swc that you don't have the source for? – Marc Hughes Jun 15 '12 at 18:51
1

Types in Events

If you use an Event in Flash, you could dispatch any named type, since it is a string. So it does not matter how you call it, as long as the listener listens to the exact same type. That's why it works. There is no magic evolved. However, I would choose to use an custom event in that case.

How custom events work

Take a look at custom events work, so you also understand where meta data comes in.

package com.website.events
{
    import flash.events.Event;

    /**
     * @author ExampleUser
    */
    public class MyCustomEvent extends Event
    {
        /** 
         * 
         */
        public static const CLOSE_WINDOW:String = "MyCustomEvent.closeWindow";


        public function MyCustomEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false):void 
        { 
            super(type, bubbles, cancelable);
        }

        override public function clone():Event 
        { 
            return new MyCustomEvent(this.type, this.bubbles, this.cancelable);
        } 

        override public function toString():String 
        { 
            return formatToString("MyCustomEvent", "type", "bubbles", "cancelable", "eventPhase"); 
        }
    }
 }

I use a static constant instead of directly a string (like your example), so it is typed more strictly.

In actionscript, you'd dispatch the custom event like this. Let's say you create a class named Window.

package com.website.ui
{
   /**
    * @eventType com.website.events.MyCustomEvent.closeWindow
   */
   [Event(name="MyCustomEvent.closeWindow", type="com.website.events.MyCustomEvent")]

import flash.display.Sprite;
import flash.events.MouseEvent;
import com.website.events.MyCustomEvent;

/**
 * @author ExampleUser
*/
public class Window extends Sprite
{
    public var mcCloseButton:Sprite;

    public function Window():void 
    { 
        this.mcCloseButton.addEventListener(MouseEvent.CLICK, handleCloseButtonClick);
    }

    private function handleCloseButtonClick(event:MouseEvent):void 
    { 
        this.dispatchEvent(new MyCustomEvent(MyCustomEvent.CLOSE_WINDOW));
    }

}
}

This is the class where the meta data should be located. This class is dispatching the event. Other classes that dispatches the same event, could have the meta-data too.

So, Window is dispatching an event with type CLOSE_WINDOW when user clicked on the close button. In another file you would listen to it and do something with it.

package com.website
{
import flash.display.Sprite;
import com.website.events.MyCustomEvent;
import com.website.ui.Window;

/**
 * @author ExampleUser
*/
public class Main extends Sprite
{
    private var _window:Window;

    public function Main():void 
    { 
        this._window:Window = new Window();
                    // a smart code-editor would give you a hint about the possible events when you typed "addEventListener" 
        this._window.addEventListener(MyCustomEvent.CLOSE_WINDOW, handleWindowClosed);
        this.addChild(this._window);
    }

    private function handleWindowClosed(event:MyCustomEvent):void 
    { 
        // do something
        this._window.visible = false;
    }
}
}

This should work. Ofcourse in a real-world situation MyCustomEvent would be named WindowEvent.

Event Meta Data

The meta data could be used to give hints to the compiler and nowadays smart code editors (FDT, FlashDevelop, FlashBuilder, IntelliJ etc.) can give code completion. It's basically a description of what kind of events may be dispatched by a class, so you know what listeners could be used.

The code should work even when the meta data is deleted.

The Event meta has a name and a type. The name should be the exact value of the type. In the case of our example, it should be the value of CLOSE_WINDOW, so that's MyCustomEvent.closeWindow. The type should be the classname with the full package, in the case of our example it would be 'com.website.events.MyCustomEvent'.

Finally, the meta data looks like this:

[Event(name="MyCustomEvent.closeWindow", type="com.website.events.MyCustomEvent")]

BTW I have some tips about your code:

  • I would suggest to use English function names and parameters, instead of Dutch.
  • verbergAdminComponent isn't a good name for a handler, it should be something like handleCloseWindow(event), which should call the verbergAdminComponent function.
Mark Knol
  • 9,663
  • 3
  • 29
  • 44