Log In  
  Getting Started  
----------------------------------------

Resources

 

Creating a Simple RIA Tutorial

Watch the video version of this tutorial.

Application Overview

previous page next page

User rating?

Show / Hide hints for:


Create a new Flex application project named FlickrRIA

1. In the Flex Builder IDE, select File > New > Flex Project and name the project "FlickrRIA".

2. Accept the default location for the project and confirm that the Application Type is Web Application and that the Server Technology is set to None.

3. Click Finish to create the project.

The FlickrRIA.mxml application file opens in the MXML editor. The editor is in Source mode.


Format the Display

1. In the opening Application tag, delete the code layout="absolute".

2. For the Application tag, add the backgroundGradientColors attribute with the value of [0xFFFFFF, 0xAAAAAA], the horizontalAlign attribute with the value of left, the verticalGap attribute with the value of 15, and the horizontalGap attribute with the value of 15.

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
	 backgroundGradientColors="[0xFFFFFF,0xAAAAAA]"
	 horizontalAlign="left"
	 verticalGap="15" horizontalGap="15" >
LEARN MORE
  • The Application container is the root XML object. It contains all other objects.
  • Download a ZIP file that contains the completed application.
  • To import the project into Flex Builder, select File > Import Flex Project.


Use Design mode to lay out the search form

1. Click the Design button to change to the Design mode. Using the Design mode is the easiest way to layout a form in Flex Builder.

2. From the Components view, drag an HBox component from the Layout folder to the design area. Keep the default values of the component. The HBox component contains the label, input field, and button for the form and displays them horizontally.

Note: The blue lines that appear in the design area help you position the component. When you release the component in the design area, it snaps into position.

3. Drag the Label component from the Controls folder to the HBox component.

4. To change the default appearance of the Label component, double-click the Label component and enter Flickr tags or search terms.

5. Drag the TextInput component from the Controls folder to the position following the Label component in the HBox. The TextInput component provides the user with a space to input search terms.

6. Drag a Button component from the Controls folder to the position following the TextInput component in the HBox component.

7. Double-click the Button component and enter Search to change the default appearance.

Create the HTTPService object

1. Change to the Source mode.

2. Use the HTTPService component to call the Flickr service and return the results. After the opening Application tag and before the HBox component, create an HTTPService component. It does not have a closing tag. To the HTTPService component, add the id attribute with a value of photoService, the url attribute with the value of [http://api.flickr.com/services/feeds/photos_public.gne], and the result attribute with the value of photoHandler(event). The photoHandler event packages the service results. We create the {{photoHandler}} function later in this tutorial.

<mx:HTTPService id="photoService"
	 url="http://api.flickr.com/services/feeds/photos_public.gne"
	 result="photoHandler(event)"/>
Working with Flickr
LEARN MORE

ASP
  • MXML files are [roughly equivalent to .aspx files.]
Key Concept: Designing a UI Layout

Create a bindable XML variable in ActionScript 3.0

1. Before the HTTPService component, add a Script component by entering <mx:Script>. Flex Builder completes the tag for you. Alternatively, you can place the Script component after the HTTPService component.

<mx:Script>
<![CDATA[

]]>
</mx:Script>

2. In the mx:Script block, enter import mx.collections.ArrayCollection. ArrayCollection is the type of object that is used as a data provider.

<mx:Script>
<![CDATA[
    import mx.collections.ArrayCollection;
]]>
</mx:Script>

3. After the ArrayCollection import statement, enter import mx.rpc.events.ResultEvent to import the ResultEvent class. The ResultEvent class is the type of event that the HTTPService generates.

<mx:Script>
<![CDATA[
    import mx.collections.ArrayCollection;
    import mx.rpc.events.ResultEvent;

]]>
</mx:Script>

4. Create a bindable private variable named photoFeed of the ArrayCollection class after the import statement in the mx:Script block. The photoFeed ArrayCollection is populated with the HTTPService response data.

<mx:Script>
<![CDATA[
    import mx.collections.ArrayCollection;
    import mx.rpc.events.ResultEvent;

    [Bindable]
    private var photoFeed:ArrayCollection;

]]>
</mx:Script>
LEARN MORE

Create the submit button click handler

1. Using the Outline view, locate the Button component in the HBox component. Clicking the Button component in the Outline view locates the Button component code in the Source mode.

2. To the Button component, add the click attribute with a value of requestPhotos(). When a user clicks the button, it calls the requestPhotos() handler, which initiates the HTTPService call.

<mx:Button label="Search" click="requestPhotos()"/>

Send the HTTPService request and keywords to the Flickr API

3. Using the Outline view, locate the TextInput component in the HBox component and add the id attribute with a value of searchTerms. The instance name for the TextInput component is id.

<mx:TextInput id="searchTerms"/>

4. In the mx:Script block, create a private function named requestPhotos() with the return value of *void. This is the function where the HTTPService call is initiated.

<mx:Script>
<![CDATA[
    import mx.collections.ArrayCollection;
    import mx.rpc.events.ResultEvent;

    [Bindable]
    private var photoFeed:ArrayCollection;

    private function requestPhotos():void{

    }
]]>
</mx:Script>

5. In the function, cancel any previous requests to photoService by using the cancel method. The instance name of the HTTPService component is photoService.

6. Create an Object variable named params.

7. Create a format parameter of the params variable with a value of rss_200_enc. This value tells Flickr how to package the response.

8. Create a tags parameter of the params variable with a value of searchTerms.text. This is the value that was entered in the the search field.

9. Send the request and params by using the send method of photoService.

<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);
    }
]]>
</mx:Script>
LEARN MORE

Key Concept:
Event listeners and callback functions let your code respond to events, such as when the user clicks with a mouse.

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.

  1. The filename is FlickrThumbnail.
  2. The component is based on VBox.
  3. 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://www.adobe.com/2006/mxml"
    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://www.adobe.com/2006/mxml"
    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.

previous page next page

Thank you Adobe team for the well organized, dynamic and useful tutorial.

The first image needs two minor corrections: "Your SEARCH returned:" and "VARIABLES Declarations".http://learn.adobe.com/wiki/download/attachments/5701667/SimpleRIAFlickrSchematic_sm.png
Regards,

Leite

Hi,

I have encountered an error while I try to run this sample scenario. Following is the error message:TypeError: Error #1010: A term is undefined and has no properties.

 This arises at the code line:private function photoHandler(event:ResultEvent):void {
photoFeed = event.result.rss.channel.item as ArrayCollection; I realized that while I tried to type in the command itself since the "event.result" did not throw up the "rss" property at all in the IDE.

 Can someone please suggest a solution?

take care,

gautam

Gautam,

   I think your problem might lie elsewhere this part looks right and works for me.  I think the reason you don't get code completion beyond event.result is that result is an Object and Object is dynamic which means properties can be added adhoc to it either that or there's a cast happening behind the scenes somewhere, but I think its the dynamic feature.  So at runtime if you get the correct instance back from the HTTPService it has the nested members rss.channel.item.  I think I'd look for something wrong in the HTTPService tag.  Hope this helps.

Joe

Tutorial worked without a hitch....

 A couple questions....

 How do I expand upon this tutorial? To be more specific, I wish to expand search results beyond the default 20 but do not see any paramater with-in the tutorial and/or code allowing you to do so. Am I missing something? How can I make the search results infinate and add next and previous page links in order to view more image search results.

 I feel the answer is easy and right in front of me but I am new to this and after hours of searching I am still at a loss. Any help would be appreciated.

If you try to query Flicker via the browser, you get 20 images:

http://api.flickr.com/services/feeds/photos_public.gne?tags=dog

So I guess this is the Flickr default. There might be some parameters that allow you to specify how many results to get and to paginate through them. But this is the Flickr API.

I have no idea about adding next and previous page links though, that's why I'm here

Posted by Cristian Ghezzi at Mar 01, 2009 08:34Updated by Cristian Ghezzi

If you want to expand your search results, you'll have to consult the Flickr API.

http://www.flickr.com/services/api/flickr.photos.search.html

I am a new to Flex 3 coming from China. I want to know what is the function of "[Bindable]"? Thx a lot for replying.

Posted by Fising at Dec 07, 2008 19:58

Placing the Bindable metadata tag before variables we can automatically manage the changes to photoFeed (the variable) and it will be instantly reflected everywhere wherever the variable is used.

Paragraph "Separate the thumbnail display to a custom component", sentence "10. Using the Outline view, locate the TileList component." should have additional reference to correct mxml tab. Full sentence should be: "10. Using the Outline view, locate the TileList component in the FlickrRIA.mxml template."
Reason for this is that in point 9 user creates new Flex Component by wizard, which, upon completion, automatically set focus on tab of newly created component. So, user could be mislead and try to find "TileList component" in Outline view of new component.

Posted by Marko Simic at Apr 25, 2009 17:18Updated by Marko Simic

Thanks for the tutorial. The code is broken apart and explained very well. When bringing in the HBox, this is confusing:"The HBox component contains the label, input field, and button for the form and displays them horizontally." Don't you want to say:"The HBox component will have the label, input field, and button added to it and it will display them horizontally."The HBox doesn't contain anything when you first add it.  It looks like the debug pane has to be open in order to step through code? I had to download flash macromedia to get the debugger to work - the downloading was flaky - took a while to get it to not fail. 
It looks like there are two places you can access the Components and Outline tools - from the Window pull down and the icon found in the bottom left corner of the IDE. If you don't know this they can be very frustrating to find.

Great Tutorial, worked perfect.

Just a question, since I'm new to Flex: is it possible to fire the search upon pressing the 'enter' key while the focus is on the textbox or at least on the 'Search' button (or, better yet, on either)? I noticed the search won't start until I explicitly click on the 'Search' button, which is somewhat awkward to those used to plain HTML forms where pressing the 'enter' key automatically submits the query.

Thanks in advance!

Andrew

Andrew,

Yes..  Simply add this to your text input field mx code:

enter="requestPhotos()"

So the entire line looks like this:

 <mx:TextInput id="searchTerms" enter="requestPhotos()"/>

 Now, when the user hits "enter", it will fire the "requestPhotos()" function.

 Hope this helps.. 

Thank you! Yes, that was very helpful.

Just another quick question: how do you handle the event of the user pressing "enter" on his keyboard when the focus is set to the submit button instead of the input box? I tried adding

enter="requestPhotos()"

to the button control but it won't compile, since the button control doesn't seem to support the "enter" attribute.

Thanks in advance,

Andrew

hi,

i m new to flex programming.can any one please explain me how data.thumbnail.url 

refers to image source..

deepak

After step 8, it says you should be able to run and test the component. When I try that it doesn't work. I get the following error.

[RPC Fault faultString="HTTP request error" faultCode="Server.Error.Request" faultDetail="Error: [IOErrorEvent type="ioError" bubbles=false cancelable=false eventPhase=2 text="Error #2032: Stream Error. URL: http://api.flickr.com/servies/feeds/photos_public.gne?format=rss%5F200%5Fenc&tags=dsf"]. URL: http://api.flickr.com/servies/feeds/photos_public.gne"]
    at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::faultHandler()\[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:220\]
    at mx.rpc::Responder/fault()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\Responder.as:53]
    at mx.rpc::AsyncRequest/fault()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:103]
    at DirectHTTPMessageResponder/errorHandler()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\messaging\channels\DirectHTTPChannel.as:362]
    at flash.events::EventDispatcher/dispatchEventFunction()
    at flash.events::EventDispatcher/dispatchEvent()
    at flash.net::URLLoader/onComplete()

However, when I continue the next steps by making the thumbnail renderer, it works. Why do we have to have the thumbnail renderer in a separate file? Is there a way to make the whole app in one file, rather than 2 mxml files?

Posted by anj at Oct 02, 2009 16:47

You have a typo:

Should be:

url = "http://api.flickr.com/services/feeds/photos_public.gne"

not

url = "http://api.flickr.com/servies/feeds/photos_public.gne"

Line 10 in 'Create the Tile component in MXML' should say "Using the Outline view, locate the TileList component in the FlickrRIA.mxml template." They don't mention that you have to go back to the other file.

Hi I'm new with FLex 3. I try to made this tutorial but I don't where I make a mistake.

I put here all my code, if some one can help me is going to be great  jejeje.

Thanks.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" backgroundGradientAlphas="[1.0, 1.0]"
    backgroundGradientColors="[#FFFFFF, #AAAAAA]" horizontalAlign="left" horizontalGap="15" verticalGap="15">
   
    <mx:Script>
<![CDATA[
    import mx.collections.ArrayCollection;
    import mx.rpc.events.ResultEvent;

    [Bindable]
    private var photoFeed:ArrayCollection;

    private function requestPhotos():void

Unknown macro: {        photoService.cancel();        var params}

   private function photoHandler(event:ResultEvent):void

Unknown macro: {        photoFeed = event.result.rss.channel.item as ArrayCollection;    }

]]>
</mx:Script>
   
   
    <mx:HTTPService id="photoService"
     url="http://api.flickr.com/services/feeds/photos_public.gne"
     result="photoHandler()"/>
   
   
    <mx:HBox>
        <mx:Label text="Flickr tags or search terms"/>
        <mx:TextInput id="searchTerms" enter="requestPhotos()"/>
        <mx:Button label="Search" click="requestPhotos(event)"/>
    </mx:HBox>
   
    <mx:TileList width="100%" height="100%"
    dataProvider="
Unknown macro: {photoFeed}
"
    itemRenderer="FlickrThumbnail">
    <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="

Unknown macro: {data.thumbnail.url}
"/>

                <mx:Text text="

Unknown macro: {data.credit}
"/>
            </mx:VBox>
        </mx:Component>
    </mx:itemRenderer>
</mx:TileList>
   
</mx:Application>
 

One thing I see is

<mx:HTTPService id="photoService"
url="http://api.flickr.com/services/feeds/photos_public.gne"
result="photoHandler()"/>

should be

<mx:HTTPService id="photoService"
url="http://api.flickr.com/services/feeds/photos_public.gne"
result="photoHandler(event)"/>

Hello,

i have done every step as instructed above, but once i press RUN i get a blank page on my internet explorer ...

Can someone please help em why is this happening?

Thank You

Hi,

Any idea how to set focus to the TextInput control?

I tried the following. The border of the control is highlighted, but I do not get the a blinking cursor:

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
backgroundGradientColors="[0xFFFFFF,0xAAAAAA]"
horizontalAlign="left"
verticalGap="15" horizontalGap="15"
updateComplete="setTextFocus()">

private function setTextFocus():void

Unknown macro: { var fm}

Thanks.

There was an error in my post. The setTextFocus function which I cannot get to show a blinking cursor is this:

private function setTextFocus():void(left brace)
var fm:FocusManager = new FocusManager(this);
fm.setFocus(searchTerms);
fm.showFocusIndicator=true;
(right brace)

It is convenient to add request action when user click enter when typing tags. You can accomplish this by adding enter parameter to TextInput:

<mx:TextInput id="searchTerms" enter="requestPhotos()"/>

Hi,

    I encounter an error when i run the application. It displays the error like "Launch Failed", I use firefox.

Below is the format of the error...............plz tell me the solution.

Login Failed
CreateProcess: "%programfiles%\mozill~1\firefox.exe -url "%1"" "C:\Documents and Settings\Administrator\My Documents\Flex Builder 3\FlickrRIA\bin-debug\FlickrRIA.html" error=2

Regards 

Added by Mark Nichoson , last edited by Randy Nielsen on Mar 31, 2008  (view change)
Labels: 
(None)

Powered by Atlassian Confluence 2.7.1, the Enterprise Wiki. Bug/feature request - Atlassian news - Contact administrators