21 December 2009
Previous experience with Flex Builder, Flash Builder, or web services will be helpful, but it is not necessary.
Beginning
Data-centric development is an exciting new Rapid Application Development (RAD) feature in Flash Builder 4, which helps traditional web and novice Flex developers to quickly build rich data-centric Flex applications that fetch data from various back-ends including ColdFusion, PHP, BlazeDS, LiveCycle Data Services ES, web services, and HTTP services.
Because web services are such a popular mechanism for exchanging data between different applications and platforms on the Internet, Flash Builder 4 provides easy and efficient ways to import, explore, and use web services. Through its unified workflow wizards, Flash Builder 4 helps developers quickly build Rich Internet Applications that leverage existing corporate or public service infrastructures, including web services.
In this article, I will demonstrate these capabilities by showing you how to build an application (see Figure 1) that uses Zenfolio's web service to display photos.
Note: The sample file ZenfolioProject.zip contains the complete source code for the project. Following the steps in this tutorial will result in a complete application, but you can download the sample file as a reference.
Follow these steps to create a new Flex project and import the Zenfolio web service.
Flash Builder will introspect the WSDL file and present a list of services, ports, and operations supported by the service.
Note: A single WSDL file can contain multiple ports for different endpoints exposed over different protocols. The web service wizard in Flash Builder 4 supports the SOAP 1.1 protocol. For this particular WSDL file, the selected port will be ZfApiHttpGet, and as a result you'll see the following notification in the dialog box, “Selected WSDL port is not compliant with SOAP 1.1 protocol. Operations and Entities cannot be read from this port. Please select a different port.”
You are presented with a list of web service operations that you can use in this service.
That's all there is to it. You have just finished importing the Zenfolio web service into your Flex project.
Follow these steps to explore the web service you just imported.
GetPopularPhotos(), the method you will use in this tutorial (see Figure 2).The GetPopularPhotos() method takes two int parameters, offset and limit, and returns an array of Photo objects.
The Photo custom data type is defined inside the service, and you can see its data structure under the Data Types node of ZenfolioService or by expanding any operation that returns a Photo type.
Now that you've seen the operations provided by the web service, you can create a simple UI that will use those operations.
The UI should include the following components:
You can create the UI shown in Figure 3 by copying the following code into ZenfolioApp.mxml:
<s:Rect radiusX="8" radiusY="8" height="67" width="194" x="11"
y="17" fill="{new mx.graphics.SolidColor(0x000000,1)}"/>
<s:Label x="21" y="34" text="Zenfolio" color="#FFFFFF" fontSize="42"/>
<s:Rect radiusX="8" radiusY="8" height="36" width="153" x="181"
y="48" fill="{new mx.graphics.SolidColor(0x000000,1)}"/>
<s:Label x="189" y="58" text="Popular Photos" color="#00DEFF" fontSize="20"/>
<s:Line y="100" x="0" width="100%" stroke="{new
mx.graphics.Stroke(0xFFFFFF,1)}"/>
<s:List id="lstPhotos" x="10" width="152"
contentBackgroundColor="#787878"
borderAlpha="0" bottom="10" top="120">
</s:List>
<s:Group bottom="10" right="10" left="170" top="120">
<mx:Image id="imgPhoto" horizontalCenter="0" verticalCenter="0"/>
</s:Group>
<s:Group height="35" width="152" top="57" right="10">
<s:Rect radiusX="8" radiusY="8" width="100%"
height="100%" fill="{new mx.graphics.SolidColor(0x000000,1)}"/>
<s:Label x="9" text="Per Page" fontSize="14" color="#FFFFFF" y="11"/>
<s:NumericStepper id="nsResults" x="76" minimum="10"
maximum="200" stepSize="5" value="50" y="6"/>
</s:Group>
GetPopularPhotos() in the List component, drag-and-drop the GetPopularPhotos() operation from the Data/Services panel to the List component.Note: You can use several alternative approaches for this step. You can right-click the List component and select Bind To Data from the context menu. Or, you can select the List component and then click the Bind To Data icon next to the Data Provider in the Properties panel or click the Bind To Data icon in the Data/Services panel.
GetPopularPhotos as the Operation and select Title as the Bind To Field setting.GetPopularPhotos() call as shown below:protected function lstPhotos_creationCompleteHandler(event:FlexEvent):void
{
GetPopularPhotosResult.token =
zefolioService.GetPopularPhotos(1,nsResults.value);
}
The generated code in ZenfolioApp.mxml calls the web service operation on the creationComplete event of the List component.
The GetPopularPhotos() operation expects two arguments, and you can pass static values or bind them to other UI components. In this case, you are passing 1 as the offset and nsResults.value (the value set in the NumericStepper control) as the limit.
If you run the project (choose Run > Run ZenfolioApp), you'll see that the photo titles are displayed in the List control, but no photos are displayed even if you select them.
Note: Calls to the web service may take several seconds to complete; some patience is required.
Follow these steps to add a change event handler on the List control that will display the image whenever the user selects an item.
lstPhotos_changeHandler() method that was generated as shown below to get the URL of the selected image and assign it to imgPhoto.source:protected function lstPhotos_changeHandler(event:IndexChangeEvent):void
{
imgPhoto.source = "http://www.zenfolio.com/"
+ lstPhotos.selectedItem.UrlCore + "-4.JPG";
}
<s:List id="lstPhotos" x="10" width="152" contentBackgroundColor="#787878"
borderAlpha="0" bottom="10" top="120"
creationComplete="lstPhotos_creationCompleteHandler(event)"
dataProvider="{GetPopularPhotosResult.lastResult}" labelField="Title"
change="lstPhotos_changeHandler(event)">
<s:itemRenderer>
<fx:Component>
<s:ItemRenderer height="100" width="90%" buttonMode="true">
<s:Group height="90" width="90%">
<s:Rect radiusX="8" radiusY="8"
width="100%" height="100%"
fill="{new mx.graphics.SolidColor(0x000000,1)}"/>
<mx:Image y="5" source="{'http://www.zenfolio.com'
+ data.UrlCore + '-1.jpg'}"
horizontalCenter="0"/>
<s:Label text="{data.Title}"
y="74" color="0xFFFFFF" width="100%" height="12"
textAlign="center"/>
</s:Group>
</s:ItemRenderer>
</fx:Component>
</s:itemRenderer>
</s:List>
Data-centric development in Flash Builder 4 helps developers quickly build Rich Internet Applications through unified workflows across various back-end technologies. It enables novice developers to get started quickly, and helps experienced developers be more productive. Data-centric development also makes advanced development methods like client side data management, paging, form generation, and chart generation easier for all Flex developers.
For more information on DCD see my introductory blog post on the topic and Tim Buntel's article Data-centric development with Flash Builder 4 beta. Sujit Reddy G also covers DCD in Flash Builder 4 on his blog.
As next steps for the sample application, you may want to add an event handler to the NumericStepper control to implement the results per page feature, or add Next and Previous buttons to navigate to all the photos from Zenfolio.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License