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

Resources

 

CRUD - Static

  • Static data served as XML files can easily be consumed by Flex applications.
  • The resultFormat property of the HTTPService component specifies the expected format of the returned data.
  • Data result handler functions can extract the returned data from the ResultEvent and cast it to an XML object.
  • The XML object can easily be used as a source in data bindings with UI components.

Before you begin

To ensure you understand the basics of requesting and posting data, please review Part II: Exchanging Data.

previous page next page

User rating?

Learn more

CRUD.MXML

In this example, a static server-side XML file is requested by the HTTPService component. The included ActionScript file contains code that casts the returned data to a Bindable XML instance that serves as the dataProvider for a DataGrid.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()">
	 <mx:Script>
		<![CDATA[
			include "employees.as";
		]]>
	</mx:Script>

	<mx:HTTPService
		id="employeesService"
		url="http://www.flexmonkeys.com/F3GSE/PartIII/CRUD/employees.xml"
		resultFormat="e4x"
		result="resultHandler(event)" />

	<mx:ViewStack id="viewstack1" width="100%" height="100%" >
		<mx:Canvas label="Form View" width="100%" height="100%">
			<mx:Form horizontalCenter="0" verticalCenter="0" backgroundColor="#FFFFFF">
				<mx:FormItem label="Query Employees ">
					<mx:Button label="Submit" click="fill()" width="100"/>
				</mx:FormItem>
			</mx:Form>
		</mx:Canvas>
		<mx:Panel label="DataGrid View" width="100%" height="100%">
			<mx:DataGrid width="100%" height="100%" dataProvider="{result.employee}">
				<mx:columns>
					<mx:DataGridColumn dataField="firstName" headerText="First Name"/>
					<mx:DataGridColumn dataField="lastName" headerText="Last Name"/>
					<mx:DataGridColumn dataField="officePhone" headerText="Phone"/>
  				</mx:columns>
			</mx:DataGrid>
		</mx:Panel>
	</mx:ViewStack>

</mx:Application>

employees.as

import mx.rpc.events.ResultEvent;

private var params:Object;

[Bindable]
private var result:XML;

private function initApp():void{
	employeesService.cancel();
	params= new Object();
}

public function resultHandler(event:ResultEvent):void {
	result = XML( event.result);
}

public function fill():void{
	viewstack1.selectedIndex=1;
	employeesService.send(params);
}
previous page next page

Try this code (I removed some unnecessary code like the call to the cancel() method and the user of hte params object). I also put it in a single file for clarity, but you can break out the ActionScript to a separate script file if you want.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:Script>
<![CDATA[
import mx.rpc.events.ResultEvent;

[Bindable]
private var r:XML;

public function fill():void {
viewstack1.selectedIndex=1;
employeesService.send();
}

public function resultHandler(event:ResultEvent):void {
r = XML(event.result);
}

]]>
</mx:Script>

<mx:HTTPService id="employeesService" useProxy="false" method="POST" url="http://www.flexmonkeys.com/F3GSE/PartIII/CRUD/employees.xml" resultFormat="e4x" result="resultHandler(event)" />

<mx:ViewStack id="viewstack1" width="100%" height="100%" >
<mx:Canvas label="Form View" width="100%" height="100%">
<mx:Form horizontalCenter="0" verticalCenter="0" backgroundColor="#FFFFFF">
<mx:FormItem label="Query Employees ">
<mx:Button label="Submit" click="fill()" width="100"/>
</mx:FormItem>
</mx:Form>
</mx:Canvas>
<mx:Panel label="DataGrid View" width="100%" height="100%">
<mx:DataGrid width="100%" height="100%" dataProvider="{r.employee}">
<mx:columns>
<mx:DataGridColumn dataField="firstName" headerText="First Name"/>
<mx:DataGridColumn dataField="lastName" headerText="Last Name"/>
<mx:DataGridColumn dataField="officePhone" headerText="Phone"/>
</mx:columns>
</mx:DataGrid>
</mx:Panel>
</mx:ViewStack>
</mx:Application>

hth,
matthew j. horn
flex docs

Hi guys,

change the name of variable "result" to "result_2" in employees.as
and then in CRUD.mxml in Line 24 also the name of the dataprovider for the datagrid:

dataProvider = "{result_2.employee}"

That will fix the problem. Matthew solved the problem also through renaming "result" to "r". It's probably a name conflict with the HTTPService result event.

I think the topest code should:

modify the :<mx:DataGrid width="100%" height="100%" dataProvider="

Unknown macro: {result.employee}
">
to
<mx:DataGrid width="100%" height="100%" dataProvider="
Unknown macro: {result.employees.employee}
">
 Because the XML structure is discripted in :
http://www.flexmonkeys.com/F3GSE/PartIII/CRUD/employees.xml
any body want to discuss with me, I am very happy to do that.
pls email me: handy.wang@finalist.cn

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

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