1 Reply Latest reply on Mar 24, 2008 9:51 AM by ntsiii

    automatic initial function on custom components


      I've spent several hours trying to find a way to realise my component. What I'd like to do is to create a custom component that will generate a number of canvas depending on the value passed to one property of my component.

      To do this I use a Repeater that uses an array which length is equal to the passed value of the property. The problem is that I need to define the length of the array depending of the value passed.

      One good and easy solution for me would be to use add a function that is executed when the component is instantiated (like a constructor), and use a for loop to fill in the Array. Such functions exist in some languuages such as init(). Does it exist too in Flex ? I've tried the initialize and creationComplete events too, but the for loop seems to be executed several times, strange behavior, filling up the Array and redefining it as empty again.

      Here is the code if you can help me. The solution might be quite easy I think, but I lack knowledge to find it.

      <?xml version="1.0" encoding="utf-8"?>
      <mx:HBox xmlns:mx=" http://www.adobe.com/2006/mxml">

      import mx.controls.Alert;

      [Bindable] public var nbrCasesTotal:uint = 0;
      [Bindable] public var nbrCasesFilled:uint = 0;
      [Bindable] private var nbrCasesEmpty:uint = 0;
      [Bindable] public var colorCase:String = "#CC0000";
      [Bindable] public var widthCase:uint = 5;
      [Bindable] public var heightCase:uint = 5;

      // emulates the loop for the Repeater using dummy values
      [Bindable ] private var dataDummy:Array = new Array();

      public function init():void{
      for (var i:int=0; i<this.nbrCasesTotal; i++){
      this.dataDummy = i;


      <mx:Repeater dataProvider="{this.dataDummy}" >



      Thanks a lot to anyone who can help
        • 1. Re: automatic initial function on custom components
          ntsiii Level 3
          The initialize or creationComplete events are where this sort of thing is normally done.

          Another solution is to use a setter function. f yo are not familiar with those, they are a way to implement a public property on a component that uses a function, so that you can perform work.

          So it would run when you passed the value into the component. this actually seems like the best way for you situation. Hereis an example (untested):

          [Bindable] private var _nbrCasesTotal:uint = 0; //the "_" is just a convention

          public function set nbrCasesTotal(value:uint):void { //note "set" is a separate word
          _nbrCasesTotal = value; //set the backing var (internal bindings will also fire)

          Typically setters are used with getters:

          public function get nbrCasesTotal():uint {
          return _nbrCasesTotal; //return backing var value