views:

2536

answers:

3

I have two components. One is called "InsideComp" and one is called "OutsideComp". OutsideComp has InsideComp as one piece of its component, and in my main MXML file, I have embedded an instance of OutsideComp. How do I access a public variable of InsideComp within my main MXML file?

In Actionscript, I could just do something like: OutsideComp.InsideComp.valToChange = 5;

But I dont know how to do it in MXML. I know this is probably an easy question.

+1  A: 

I'm updating the code here to include a reference to the outer class. I'm not 100% certain this is what you're looking for, but I'll do my best to give you a

OuterClass:

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:local="*">
    <local:InnerClass id="inner" width="100%" height="100%" />
</mx:VBox>

InnerClass:

<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"&gt;
    <mx:CheckBox id="innerCheckbox" selected="true" />
</mx:VBox>

Edit: Here's the updated version of the Application

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
    layout="horizontal" xmlns:local="*">

    <mx:Binding source="{outer.inner.innerCheckbox.selected.toString()}"
                destination="checkLabel.text" />
    <mx:Label id="checkLabel" />
    <local:OuterClass id="outer" width="100%" height="100%" />
</mx:Application>

Here's a brief explanation of what this does:

  1. There are 3 MXML files:

    • OuterClass: an MXML file which contains InnerClass
    • InnerClass: an MXML file which contains a checkbox
    • Application: the main app which contains the OuterClass
  2. There is a binding in the Main app which takes the checkbox value (via the Object hierarchy) and sets the Label's text field appropriately. This works just like ActionScript would: with the . operator to access nested objects.

  3. When the checkbox updates, the value of the Label updates accordingly.

Hope this makes things a little clearer.

bedwyr
I dont see you accessing the 'OutsideComp' in this example. It looks like you're just embedding the InnterComp. Is that right?
Seidleroni
If you're still working on this (no worries if you're not interested -- I'm just trying to help :), see the latest edits.
bedwyr
+4  A: 

By setting the id property of the MXML component you effectively make it a public property accessible via dot-notation. "Accessing it via MXML" is sort of a trick question. You can use binding notation within an xml tag and bind the property to another property, or you can access it in your Script block in the normal AS3 fashion.

Joel Hooks
Are you saying I should create code inside my OuterComp to bind its properties to InnerComp's properties? This is the route I was thinking about, but didnt seem like it was the best option (thought there may be a better way to do it)
Seidleroni
there is almost always a better way of doing it, but I have used this pattern before. The InnerComp has a property that is bound in the OuterComp to give you convenient top-level access to the property. It tightly couples the components, essentially making them a single component, but it works.
Joel Hooks
Doing it this way does not create a "burden" on the application? It is not going to slow the application down more than another approach?
Seidleroni
It won't slow your application at all.
Joel Hooks
A: 

I think the most direct way to answer this is to use a script tag. This will allow you to run the AS you're familiar with. First, you'll need to assign ids for each of the properties you want to edit.

<mx:Script>
   private function onCreationComplete (event:Event):void {
     idOfTheOutsideComp.idOfTheInsideComp.propertyName = newValue;
   }
</mx:Script>

I put this code inside of a creation complete handler because you can't simply run the code from within the Script tags, it needs to be run within a function.

Mims H. Wright