It's not good coupling from an OOP standpoint to reference inner
children in that manner.

That is, the slideshow shouldn't "know" about my_hbox and my_txt.  By
having those references inside slideshow, you limit what children it can
be displayed.

A better approach is to create an interface, and have the slideshow
children all implement that interface:

// SlideShowChild.as
public interface SlideShowChild
{
        function get label():String;
        function set label( value:String );
}

// ExampleChild.mxml - note that we're implementing the
// interface here
<mx:HBox implements="SlideShowChild">
        <mx:Script>
                public function get label():String
                {
                        return description.text;
                }

                public function set label( value:String )
                {
                        description.text = value;
                }
        </mx:Script>

        <mx:Text id="description" />
</mx:HBox>

Now, inside slideshow, we can cast each child as being a SlideShowChild
instance, and call the methods that we know about:

// Note here that all of the children are typed as the interface
// and not as any particular class instance (like ExampleChild).
for each ( var child:ShildShowChild in children )
{
        child.label = "setting child's label from outer slideshow";
}

By taking this approach, you have an interface that acts as the contract
between the slideshow and the children being displayed.  The slideshow
itself doesn't know about the implementation details of the child, but
it knows that every child has to have a certain set of
methods/properties that can be called.  You can set the label of the
child without knowing that the child has a "description" text field.

Sorry for writing code in email, it probably won't compile, but this
should give you an idea of a better approach to take.

-d


James Battat wrote:
> Hi,
> 
> I have a custom class (SlideShow) that I'm including in an MXML document.
> In the same MXML document, I add several UIComponents as children of 
> SlideShow.
> I would like the SlideShow class to be able to access those UIComponents 
> (e.g. change the text of a Text object).  Right now, I am stepping through 
> children to get the references to each UI object.  But a change in the layout 
> will break my application.  Is there a way to get a 
> child/subchild/subsubchild/etc without trawling through the hierarchy (sort 
> of like the double dot operator in XML parsing)?
> 
> Here is my example:
> 
> <ss:SlideShow id="slideshow" width="800" height="600">
>   <mx:HBox id="my_hbox"> 
>       <mx:Text id="my_txt" text="Description Goes Here" />
>   </mx:HBox>
> </ss:SlideShow> 
> 
> To get the Text object, I am currently doing:
>    var my_hbox:HBox = HBox(getChildByName("main_hbox"));
>    var my_txt:Text  = Text(my_hbox.getChildByName("my_txt"));
> 
> But it's clear that this is not a good way to do it because if I want to 
> change the MXML code (make the HBox a VBox or add more HBoxes, etc) then the 
> "path" to the Text object will change.  
> 
> I'm looking for a solution that is more like:
>   var my_txt:Text = Text(getChild("my_txt"));
> which would return a reference to the Text object irrespective of where it 
> lives in the child hierarchy.
> 
> Thank you very much in advance for your help,
> James
> 
> 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Enterprise web applications, build robust, secure 
scalable apps today - Try it now ColdFusion Today
ColdFusion 8 beta - Build next generation apps

Archive: http://www.houseoffusion.com/groups/Flex/message.cfm/messageid:4501
Subscription: http://www.houseoffusion.com/groups/Flex/subscribe.cfm
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.37

Reply via email to