14 Replies Latest reply on May 11, 2009 8:58 AM by Gregory Lafrance

    Can I define a constructor for a Custom Component?

    apmyhr

      I have a custom component which I instantiate through ActionScript.  For the sake of clean code, I would like to be able to assign the variables through the constructor like any other class:

       

      var myComp:CustomComponent = new CustomComponent(arg1, arg2, ...);

       

      However, when I try to write a constructor in the Script block for the component, it gives me a compile error telling me that I have multiple constructors:

       

      //In Script tag//

      public function CustomeComponent(arg1, arg2 ...):void { ... }

       

      Are we not able to define constructors for Custom Components?

        • 1. Re: Can I define a constructor for a Custom Component?
          JeffryHouser Level 4

          If it is an ActionScript component, you should have no problem creating and modifying a constructor.

           

          For MXML components, you cannot add your won constructor because it would conflict with the one that the compiler creates.

          • 2. Re: Can I define a constructor for a Custom Component?
            Barna Biro Level 3

            Hi there,

             

            Jeffry is correct. In case you still want to use the component as an MXML structure then you should create it from AS first and create an MXML component that inherits from your newly created AS component. That way, "you'll have the power of two in one".

             

            With best regards,

            Barna Biro

            • 3. Re: Can I define a constructor for a Custom Component?
              apmyhr Level 1

              Thanks Jeffry.  I decided I'll go with the ActionScript approach to the custom component.  Unfortunately this means I have to figure out how to replicate my bindings in ActionScript in an clean and elegant way.

              • 4. Re: Can I define a constructor for a Custom Component?
                Barna Biro Level 3

                If this will remain a Flex project then you can use Binding inside AS classes/files too. Binding is not supported only inside the Flash IDE ( if I'm not mistaking ) and in "pure ActionScript projects".

                 

                With best regards,

                Barna Biro

                • 5. Re: Can I define a constructor for a Custom Component?
                  apmyhr Level 1

                  Yes, I'm using the BindingUtils class to bind my variables now.  I was just lamenting the fact that now i cant write a simple bind like x = "{width / 2}".  Instead I have to write BindingUtils.bindSetter(functionName, this, "width"}; private function functionName(newWidth) { x = newWidth / 2; }

                   

                  If you know of a cleaner and simpler way to do these type of binds in ActionScript, please let me know.

                  • 6. Re: Can I define a constructor for a Custom Component?
                    JeffryHouser Level 4

                    For some reason, I don't make use of binding inside my AS3 components.

                     

                    It is more likely that I'll expand all my properties to get and set methods, and make use the invalidation methods to trigger lifecycle methods, and handle property changes in such methods.

                     

                    But, I guess it depends what your binding sources and destinations are.

                    • 7. Re: Can I define a constructor for a Custom Component?
                      Barna Biro Level 3

                      apmyhr wrote:

                       

                      Yes, I'm using the BindingUtils class to bind my variables now.  I was just lamenting the fact that now i cant write a simple bind like x = "{width / 2}".  Instead I have to write BindingUtils.bindSetter(functionName, this, "width"}; private function functionName(newWidth) { x = newWidth / 2; }

                       

                      If you know of a cleaner and simpler way to do these type of binds in ActionScript, please let me know.

                       

                      Or you could simply declare a variabled xPost that is Bindable and simply assign it to the value of x;

                       

                      [Bindable]
                      private var _xPos:Number = 10;
                      
                      /* ---- code comes here ---- */
                      
                      x = _xPos;
                      
                      /* ---- more code comes here --- */
                      

                       

                      That should work too.

                      • 8. Re: Can I define a constructor for a Custom Component?
                        Gregory Lafrance Level 6

                        Actually you can simulate constructors in MXML components using a set method:

                         

                        <?xml version="1.0" encoding="utf-8"?>
                        <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                          xmlns:comp="*">
                          <comp:MyVBox myLabel="Hello"/>
                        </mx:Application>

                         

                        <?xml version="1.0" encoding="utf-8"?>
                        <mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml">
                          <mx:Script>
                            <![CDATA[
                              public function set myLabel(str:String):void{
                                this._myLabel.text = str;
                              }
                            ]]>
                          </mx:Script>
                          <mx:Label id="_myLabel" fontSize="20"/>
                        </mx:VBox>

                         

                        Of course, in this case my code is not working, and I don't know why, because I've done this many times before. Oh well, guess it's Monday.

                         

                        If this post helps, please mark it as such.

                        • 9. Re: Can I define a constructor for a Custom Component?
                          apmyhr Level 1

                          I'm pretty sure that doesn't work in ActionScript.  If you say x = _xPos all you are doing is assigning x to the value of _xPos at that specific time.  Therefor, if _xPos changes afterwards, x will not be updated.  Please correct me if I am wrong.

                          • 10. Re: Can I define a constructor for a Custom Component?
                            apmyhr Level 1

                            Greg, I'm not sure that would address what I wanted.  I wanted to declare and instantiate the component in ActionScript, basically:

                             

                            var myComp:CustomComp = new CustomComp(arg1, arg2, ...);

                             

                            So I don't think your example would work for that.  Or would it?

                            • 11. Re: Can I define a constructor for a Custom Component?
                              Gregory Lafrance Level 6

                              In this case it is a "set" method, a common AS construct for calling methods with variable assignment syntax.

                              • 12. Re: Can I define a constructor for a Custom Component?
                                JeffryHouser Level 4

                                That isn't exactly a constructor; it is setting a property.

                                 

                                I can see how that would work in some situation, though.  By the same token you could also respond to the preinitialize event.

                                • 13. Re: Can I define a constructor for a Custom Component?
                                  Barna Biro Level 3

                                  @ apmyhr: it seems that you are correct. I never used binding in a custom AS component before but I assumed that it would work. It seems that my assumption was incorrect. Hmm...

                                  • 14. Re: Can I define a constructor for a Custom Component?
                                    Gregory Lafrance Level 6

                                    If this post helps, please mark it as such.

                                     

                                    If you create an array variable in your MXML component, and then set it with myVar="[val1, val2, val3]" in the opening tag of your component, then you basically have a constructor in MXML.

                                     

                                    You can use the [Bindable] metadata tag in three places:

                                     

                                    Before a public class definition.
                                    The [Bindable] metadata tag makes usable as the source of a binding expression all public properties that you defined as variables, and all public properties that are defined by using both a setter and a getter method. In this case, [Bindable] takes no parameters, as the following example shows:

                                     

                                    [Bindable]
                                    public class TextAreaFontControl extends TextArea {}

                                     

                                    The Flex compiler automatically generates an event named propertyChange, of type PropertyChangeEvent, for all public properties so that the properties can be used as the source of a data binding expression.

                                     

                                    If the property value remains the same on a write, Flex does not dispatch the event or update the property, where not the same translates to the following test:

                                     

                                    (oldValue !== value)

                                     

                                    That means if a property contains a reference to an object, and that reference is modified to reference a different but equivalent object, the binding is triggered. If the property is not modified, but the object that it points to changes internally, the binding is not triggered.

                                     


                                    Note: When you use the [Bindable] metadata tag before a public class definition, it only applies to public properties; it does not apply to private or protected properties, or to properties defined in any other namespace. You must insert the [Bindable] metadata tag before a nonpublic property to make it usable as the source for a data binding expression.

                                     

                                    Before a public, protected, or private property defined as a variable to make that specific property support binding.
                                    The tag can have the following forms:

                                     

                                    [Bindable]
                                    public var foo:String;

                                     

                                    The Flex compiler automatically generates an event named propertyChange, of type PropertyChangeEvent, for the property. If the property value remains the same on a write, Flex does not dispatch the event or update the property.

                                     

                                    You can also specify the event name, as the following example shows:

                                     

                                    [Bindable(event="fooChanged")]
                                    public var foo:String;

                                     

                                    In this case, you are responsible for generating and dispatching the event, typically as part of some other method of your class. You can specify a [Bindable] tag that includes the event specification if you want to name the event, even when you already specified the [Bindable] tag at the class level.

                                     

                                    Before a public, protected, or private property defined by a getter or setter method.
                                    You must define both a setter and a getter method to use the [Bindable] tag with the property. If you define just a setter method, you create a write-only property that you cannot use as the source of a data-binding expression. If you define just a getter method, you create a read-only property that you can use as the source of a data-binding expression without inserting the [Bindable] metadata tag. This is similar to the way that you can use a variable, defined by using the const keyword, as the source for a data binding expression.

                                     

                                    The tag can have the following forms:

                                     

                                    As far as binding, you can add the [Bindable] tag before the class declaration to make bindable all public properties defined as variables, and all public properties defined by using both a setter and a getter method.