3 Replies Latest reply on Aug 15, 2007 6:36 PM by Peter Flynn (Adobe)

    passing a reference / variable to a Custom Component

    ljonny18 Level 1
      Hi, I was wondering if someone could help me.

      It seems like a very simple problem but I cant for the life of me seem to work out a solution.


      I have created a Custom Component that extends from the UIComponent that consists of a “rev counter” style clock face etc….
      I want to use this component multiple times within my application – however feed it different data so for example each “rev counter” on the page will be displaying different data etc…..

      I want to be able to reuse my component and not have to create a new component every time I add a “rev counter” to my application, therefore I need some way of passing a reference / variable to the component (maybe from the <mx /> tag where I declare it in the MXML code ??? )

      <mx:Application …. xmlns:comps="components.*" …..>

      <comps:RevCounterComp id=”” (add something here to reference???) />

      </ mx:Application>



      Doing this will allow me to reuse my ONE custom component MANY times feeding it different data (different data provaiders for each instance of the single component)

      Hope this makes sense???

      Any help / advice is much appreciated,

      Thanks,
      Jon.
        • 1. Re: passing a reference / variable to a Custom Component
          connectricity Level 1
          If you want to instantiate your custom component you are goign to want to create it dynamically in actionscript:

          var myComp:MyCustomComponent = new MyCustomComponent();
          whatEver.addChild(myComp);
          var myComp2:MyCustomComponent = new MyCustomComponent();
          whatEver.addChild(myComp2);

          If you want to be able to refer to them from other functions you can declare them public.
          • 2. Re: passing a reference / variable to a Custom Component
            jmryan Level 1
            Simply declaring a public property inside your custom component (either a public var or public get/set methods) will allow you to pass additional data to your component. Put the variables in an <mx:Script /> block if it's an MXML component. Again, make sure they are public or have public getter/setter methods.
            • 3. Re: passing a reference / variable to a Custom Component
              Peter Flynn (Adobe) Adobe Employee
              Jon,

              jmryan's suggestion is the preferred way to go. This way you can use the simple MXML syntax that you described in your post.

              If your custom component is defined in ActionScript, you can use setters or the creationComplete event to update the component when the values are passed in from the tag (they are set *after* your constructor runs).

              Even easier, if your component is defined in MXML, you can add [Bindable] to your public field and then bind directly to it in the custom component's MXML code.

              - Peter