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:
- <mx:Applicationxmlns:mxmx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute">
-
- <mx:HTTPService
- id="productsRequest"
- url="http://www.somesite.com/products.php"/>
- ...
-
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:
- <mx:DataGridxmx:DataGridx="20"y="80"id="productGrid"width="400"
- dataProvider="{productRequest.lastResult.products.items}">
- <mx:columns>
- <mx:DataGridColumnheaderTextmx:DataGridColumnheaderText="Name"dataField="name"/>
- <mx:DataGridColumnheaderTextmx:DataGridColumnheaderText="Price"dataField="price"/>
- </mx:columns>
- </mx:DataGrid>
-
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:
- <XML>
- <products>
- <item>
- <name>MobilePhone</name>
- <price>$199</price>
- </item>
- <item>
- <name>CarCharger</name>
- <price>$34</price>
- </item>
- ...
-
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:
- <mx:Applicationxmlns:mxmx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute"
- 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:
- <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.