Create the HTTPService result handler
10. After the requestPhotos() function, create a private function named photoHandler and pass the event of type ResultEvent to the function. The return type is void. The photoHandler handles the response from the HTTPService call.
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
private var photoFeed:ArrayCollection;
private function requestPhotos():void{
photoService.cancel();
var params:Object = new Object();
params.format = 'rss_200_enc';
params.tags = searchTerms.text;
photoService.send(params);
}
private function photoHandler(event:ResultEvent):void{
}
]]>
</mx:Script>
Populate the photoFeed XML variable
11. In the photoHandler() function, populate the photoFeed variable with the data located in the event object, event.result.rss.channel.item, and type it as ArrayCollection
<mx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
[Bindable]
private var photoFeed:ArrayCollection;
private function requestPhotos():void{
photoService.cancel();
var params:Object = new Object();
params.format = 'rss_200_enc';
params.tags = searchTerms.text;
photoService.send(params);
}
private function photoHandler(event:ResultEvent):void{
photoFeed = event.result.rss.channel.item as ArrayCollection;
}
]]>
</mx:Script>
Create the Tile component in MXML
1. Use a TileList component to display the images. After the HBox component and before the closing Application tag, add a TileList component with a width of 100% and height of 100%.
<mx:TileList width="100%" height="100%">
</mx:TileList>
Bind the photoFeed XML data to the TileList component
2. Using the Outline view, locate the TileList component and add an attribute of dataProvider with a value of {photoFeed} to bind the data to the tile component. (Remember to move the > to the end of the dataProvider line.)
<mx:TileList width="100%" height="100%"
dataProvider="{photoFeed}">
</mx:TileList>
Create the thumbnails item renderer in the Tile component
3. The item renderer renders the layout for each item in the TileList. Within the TileList component, add a itemRenderer property.
<mx:TileList width="100%" height="100%"
dataProvider="{photoFeed}">
<mx:itemRenderer>
</mx:itemRenderer>
</mx:TileList>
4. Create a layout component for the item renderer. Within the itemRenderer property, add a Component component.
<mx:TileList width="100%" height="100%"
dataProvider="{photoFeed}">
<mx:itemRenderer>
<mx:Component>
</mx:Component>
</mx:itemRenderer>
</mx:TileList>
5. Create the layout the item renderer will use. Within the Component, add the VBox component with attributes of width with a value of 125, height with a value of 125. Add paddingBottom, paddingTop, paddingRight and paddingLeft each with a value of 5
<mx:TileList width="100%" height="100%"
dataProvider="{photoFeed}">
<mx:itemRenderer>
<mx:Component>
<mx:VBox width="125" height="125"
paddingBottom="5"
paddingLeft="5"
paddingTop="5">
</mx:VBox>
</mx:Component>
</mx:itemRenderer>
</mx:TileList>
6. Within the VBox component, create a Image component. Add the attributes width with a value of 75, height with a value of 75. The itemRenderer passes values to the Image component through the Image component's data property. Add a source with a value of {data.thumbnail.url} to the Image component to populate the image.
<mx:TileList width="100%" height="100%"
dataProvider="{photoFeed}">
<mx:itemRenderer>
<mx:Component>
<mx:VBox width="125" height="125"
paddingBottom="5"
paddingLeft="5"
paddingTop="5"
paddingRight="5">
<mx:Image width="75" height="75"
source="{data.thumbnail.url}"/>
</mx:VBox>
</mx:Component>
</mx:itemRenderer>
</mx:TileList>
7. After the Image component, create a Text component with the text attribute having a value of {data.credit} to display the name of the author.
<mx:TileList width="100%" height="100%"
dataProvider="{photoFeed}">
<mx:itemRenderer>
<mx:Component>
<mx:VBox width="125" height="125"
paddingBottom="5"
paddingLeft="5"
paddingTop="5"
paddingRight="5">
<mx:Image width="75" height="75"
source="{data.thumbnail.url}"/>
<mx:Text text="{data.credit}"/>
</mx:VBox>
</mx:Component>
</mx:itemRenderer>
</mx:TileList>
8. Save then run the application. You should see a form. Submit a search term. You should see the application display images.
Separate the thumbnail display to a custom component
9. Create a new component: File > New > MXML Component.
- The filename is FlickrThumbnail.
- The component is based on VBox.
- Set the width to 125 and the height to 125.
10. Using the Outline view, locate the TileList component.
11. Cut the Image and Text components from the VBox component in TileList, and paste them into FlickrThumbnail.mxml.
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http:
width="125" height="125">
<mx:Image width="75" height="75"
source="{data.thumbnail.url}"/>
<mx:Text text="{data.credit}"/>
</mx:VBox>
12. Add the following attributes to the VBox component: paddingBottom, paddingTop, paddingRight, and paddingLeft each with a value of 5; horizontalScrollPolicy and verticalScrollPolicy, both with a value of off; and horizontalAlign with a value of center.
<?xml version="1.0" encoding="utf-8"?>
<mx:VBox xmlns:mx="http:
width="125" height="125"
paddingBottom="5" paddingLeft="5" paddingTop="5" paddingRight="5"
horizontalScrollPolicy="off" verticalScrollPolicy="off"
horizontalAlign="center">
<mx:Image width="75" height="75"
source="{data.thumbnail.url}"/>
<mx:Text text="{data.credit}"/>
</mx:VBox>
13. Using the Outline view, locate the TileList component in the FlickrRIA.mxml template.
14. Delete the code for the itemRenderer, Component, and VBox components.
15. Add the attribute itemRenderer to the TileList component with a value of FlickrThumbnail.
<mx:TileList width="100%" height="100%"
dataProvider="{photoFeed}"
itemRenderer="FlickrThumbnail">
</mx:TileList>
16. Compile and run the application.

|