When I decided to learn Flex I started with
Learning Flex 3. It's a great book and does an excellent job of introducing the MXML framework and Flex Builder IDE. I would recommend it to anyone.
One of the problems I have with the book is it doesn't teach you how to remove warnings from your code. A later code example produces the following error:
warning: unable to bind to property 'contact' on class 'XML' (class is not an IEventDispatcher). It's not a big deal, but I like to write code that doesn't produce hidden errors.
Here is the code from the book:
<mx: Application
xmlns: mx="http: //www.adobe. com/2006/mxml"
xmlns: view="com.oreilly.view.*"
layout="absolute"
applicationComplete="contactsService.send()" >
<mx:HTTPService id="contactsService"
resultFormat="e4x"
url="contacts.xml" />
<mx:DataGrid id="contactsDataGrid"
dataProvider="{contactsService.lastResult.contact}"
selectedIndex="0"
left="10"
top="10"
bottom="10"
width="300">
<mx: columns>
<mx: DataGridColumn headerText="First"
dataField="firstName"/>
<mx: DataGridColumn headerText="Last" dataField="lastName"/>
</mx:columns>
</mx:DataGrid>
<view: ContactViewer
contact="{contactsDataGrid. selectedItem}"
x="318" y="10">
</view: ContactViewer>
</mx: Application>
The problem is the
dataProvider property of DataGrid. It's unable to figure out how to parse the data, so it throws a warning. This can be resolved with casting. We can tell the dataProvider what it should be and it will remove the warning. Here is the code that will remove the warning.
dataProvider="{XMLList(XML(contactsService.lastResult).contact)}"
contactsService.lastResult is an XML object. There are multiple children in the object. We can then cast the XML object as an
XMLList to use contact child node.
Feel free to leave a comment if you have any questions.
Labels: cast, casting, class, datagrid, dataprovider, error, flex, ieventdispatcher, lastResult, learning, warning, xml