5 Replies Latest reply on Jun 3, 2010 9:10 AM by msakrejda

    How to call constructor in MXML?

    hezjing Level 1

      Hi

       

      If I have created a custom component in ActionScript that has non-empty constructor, how do I create this component in MXML?

        • 1. Re: How to call constructor in MXML?
          KishCq5@@ Level 1

          Generally if you create an instance of class defaultly constructor of the class will be executed.

           

          As you are creating an custom component mean you may be extending it with some flex component.the custom component will be having the properties of the component extended.In MXML you can directly call that.

          • 2. Re: How to call constructor in MXML?
            JeffryHouser Level 4

            If I understand properly, you want to re-create an ActionScript component in MXML; is that correcT?

             

            MXML doesn't have constructors.  You should move the "constructor" code to a preinitialize event handler.

            • 3. Re: How to call constructor in MXML?
              hezjing Level 1

              Yes, so imagine that I have a panel which looks like the following:

               

                  public class MyPanel extends VBox

                  {

                      public function DummyComp (title:String)

                      {

                      }

                  }

               

              and I want to ensure that the user pass in the title when creating this panel in ActionScript or MXML.

               

              • 4. Re: How to call constructor in MXML?
                Sebastien V. Level 3

                Then you can not use MXML tag to create the component.

                You need to create the component via ActionScript ( new MyComponent( title ) ) in your MXML file Script part, and place it where it is supposed to be with the addChild method of its parent tag.

                • 5. Re: How to call constructor in MXML?
                  msakrejda Level 4

                  Alternately, if you can provide a default value for all constructor arguments, you *can* declare the component in MXML. E.g.,

                   

                       public class MyPanel extends VBox

                      {

                           public function DummyComp (title:String='untitled')

                          {

                          }

                      }

                   

                  You can then use

                   

                      <mycomps:DummyComp/>

                   

                  You'll probably also want to expose a property for title so you can set it after it's constructed. Unfortunately, this makes it pretty much impossible to create immutable properties. If you implement IMXMLComponent (or whatever that interface is called), you'll get a callback for the initialize() method once the component's id and parent container, but this can happen before all properties are initialized (!), which makes it a lot less useful.