views:

126

answers:

2

I've written the following custom component, SubNavBar.mxml:

<?xml version="1.0" encoding="utf-8"?>
<mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" height="100" width="300"
 creationComplete="init()">

 <mx:Script>
  <![CDATA[
   import mx.collections.XMLListCollection;

   [Bindable] public var menuItems:XMLListCollection;

   private function init():void
   {
    trace("SubNav: config = "+menuItems);
   }


  ]]>
 </mx:Script>

 <mx:HBox y="30" id="menu">
  <mx:List dataProvider="{menuItems}"/>
 </mx:HBox>

</mx:Canvas>

I setup this component in a parent custom component using the following code:

<com:SubNavBar id="subNavMenu" menuItems="{subNavConfig}"
 x="10" y="-15">
</com:SubNavBar>

Whenever the trace function runs in init(), the property menuItems returns null. I don't seem to have this problem with other variable types, like Boolean or String. Is this due to the size of the XMLListCollection object? How can I set up this SubNavBar custom component with XMLListCollection property and bind it to a control in the component?

Thanks!

A: 

Maybe I am missing something, but where are you setting {subNavConfig} ?

Edit:

It could be because of how it is being casted...try something like...

var listcol:XMLListCollection = new XMLListCollection(configXML.destination.(@mapID == mapID).subSections);
Chris Gutierrez
{subNavConfig} is a property of the parent custom component, Destination.mxml. This property is set in main.mxml when Destination is instantiated, e.g. "destination.subNavConfig = configXML.destination.(@mapID == mapID).subSections as XMLListCollection". configXML is the the event result of an HTTPrequest to a local setup.xml file.
Scott Heath
I've posted an edit.
Chris Gutierrez
A: 

I tested out this code and everything seemed to work for me. Are you sure you're setting up the subNavConfig variable properly?

Here's the client code I used:

// Test.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="onInit()" xmlns:ns="comp.*">
  <mx:Script>
    <![CDATA[
      import mx.collections.XMLListCollection;

      [Bindable]
      public var bookCollection:XMLListCollection;

      public function onInit():void
      {
        var books:XML = <books>
                          <book publisher="Addison-Wesley" name="Design Patterns" />
                          <book publisher="Addison-Wesley" name="The Pragmatic Programmer" />
                          <book publisher="Addison-Wesley" name="Test Driven Development" />
                          <book publisher="Addison-Wesley" name="Refactoring to Patterns" />
                          <book publisher="O'Reilly Media" name="The Cathedral & the Bazaar" />
                          <book publisher="O'Reilly Media" name="Unit Test Frameworks" />
                        </books>;

        var booklist:XMLList = books.book;
        bookCollection = new XMLListCollection(booklist);
      }
    ]]>
  </mx:Script>

  <ns:SubNavBar id="fb" menuItems="{bookCollection}"/>
</mx:Application>

And here's the output I got:

SubNav: config = <book publisher="Addison-Wesley" name="Design Patterns"/>
<book publisher="Addison-Wesley" name="The Pragmatic Programmer"/>
<book publisher="Addison-Wesley" name="Test Driven Development"/>
<book publisher="Addison-Wesley" name="Refactoring to Patterns"/>
<book publisher="O'Reilly Media" name="The Cathedral &amp; the Bazaar"/>
<book publisher="O'Reilly Media" name="Unit Test Frameworks"/>
Wesley Petrowski
Thanks for taking a look at my issue.I setup a new project to test out your code and I'm still getting "SubNav: config = null"; however, I added a DataGrid control to the SubNav component and it renders with the data you specified in the books XML variable. The SubNav component seems to be binding the XMLListCollection, but not in time for the trace() function in the SubNav's init().Any thoughts?
Scott Heath
Just to double check, are you calling onInit() on creationComplete in main.xml?
Scott Heath
Actually, I am calling onInit() in the initialize event handler. I've updated the answer to include the Application tag.The creationComplete event only fires once a component and all its children have been created, so by that time the init() function in the SubNavBar has already executed. You can find some more info about the order events happen at http://livedocs.adobe.com/flex/3/html/help.html?content=ascomponents_advanced_2.html
Wesley Petrowski
This might also be of use: http://www.mikaflex.com/?p=270
Wesley Petrowski