Learning notes how to connect Flex data

Source: Internet
Author: User

This article focuses on how to connect Flex data. In Flex, you can use several methods to manipulate and manage data, most of their related content is beyond the scope of the brief introduction here.

Connect to Flex data

Remember that the most important thing for data operations in a Flex application is that the Flex application is not directly connected to a database. Therefore, Adobe? FlexBuilder2 does not provide tools to directly connect to Data. You can use MXML and ActionScript code to operate and manage data.

In Flex, you can use several methods to manipulate and manage data. Most of the relevant content is beyond the scope of the brief introduction here, see the following chapters ). However, imagine a method that can connect external data. The subsequent example will show how to connect to the data in the XML structure.

Flex data generation

Because the Flex application is not directly connected to a database, you need to use a certain type of service to support data usage. In the following example, the HTTPService component is used to obtain data from an XML file generated by PHP.

The first step to connect to the Flex database is to generate the data that will be used in the Flex program. In applications with PHP, you will take the following steps:

1. Create a database such as MySQL ).

2. Write a PHP script to connect to the MySQL database and generate data in XML format.
These steps are also suitable for data generated on other work platforms, such as ASP. NET and JSP ).

Connect external data sources

For XML format data generated by PHP, you can use the HTTPService component to request data, as shown in the following code:

 
 
  1. <mx:Applicationxmlns:mxmx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute"> 
  2.  
  3. <mx:HTTPService 
  4. id="productsRequest" 
  5. url="http://www.somesite.com/products.php"/> 
  6. ...  
  7.  

The HTTPService component defines a request ID. You can use this ID to control the URL of the provided data or the binding between the server and the data.

Binding of external Flex data to data-driven control

By binding data to data-driven control data-drivencontrol), you can process the HTTP service result XML data), just like this:

 
 
  1. <mx:DataGridxmx:DataGridx="20"y="80"id="productGrid"width="400"  
  2. dataProvider="{productRequest.lastResult.products.items}"> 
  3. <mx:columns> 
  4. <mx:DataGridColumnheaderTextmx:DataGridColumnheaderText="Name"dataField="name"/> 
  5. <mx:DataGridColumnheaderTextmx:DataGridColumnheaderText="Price"dataField="price"/> 
  6. </mx:columns> 
  7. </mx:DataGrid> 
  8.  

The Data Binding syntax is displayed in the wavy brackets in the dataProvider attribute of Data Control. It contains the HTTPService Request ID, lastResult method, and XML file data structure. In this example, the data structure of the XML data source looks like this:

 
 
  1. <XML> 
  2. <products> 
  3. <item> 
  4. <name>MobilePhone</name> 
  5. <price>$199</price> 
  6. </item> 
  7. <item> 
  8. <name>CarCharger</name> 
  9. <price>$34</price> 
  10. </item> 
  11. ...  
  12.  

By setting the dataField attribute, the project data name and price are used as the data of each column in the data grid.

Load Flex data at runtime

You can also load data when the Flex program starts running, as shown later, sending a request to a specific URL in HTTPService:

 
 
  1. <mx:Applicationxmlns:mxmx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute"  
  2. creationComplete="productsRequest.send()"> 

After you add the creationComplete method to the application tag, the data is loaded at runtime and handed in to the data driver control. In this example, the data grid is used ).
You can also add an HTTPService request to a control event rather than a tag in the program, as shown below:

 
 
  1. <mx:Buttonxmx:Buttonx="50"y="8"label="GetData"click="productsRequest.send();"/> 

Flex provides many methods to connect, manage, format, and verify data. You can use remote program calls, data services, or other enterprise-level technologies to manipulate and manage data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.