Why are my XMLListCollection properties in my custom components always null?
-
19-09-2019 - |
Question
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!
Solution
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 & the Bazaar"/>
<book publisher="O'Reilly Media" name="Unit Test Frameworks"/>
OTHER TIPS
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);