3.1.1 The mxml file
This is the main.mxml file of our comple example:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" backgroundColor="#FFFFFF"> <mx:RemoteObject id="myservice" source="tutorials.PersonService" destination="amfphp" fault="faultHandler(event)" showBusyCursor="true"> <mx:method name="getList" result="getListHandler(event)" fault="faultHandler(event)" /> </mx:RemoteObject> <mx:DataGrid x="10" y="10" width="345" id="people_list" dataProvider="{dp}" change="changeHandler(event)"> <mx:columns> <mx:DataGridColumn headerText="Last name" dataField="lastName"/> <mx:DataGridColumn headerText="First name" dataField="firstName"/> <mx:DataGridColumn headerText="Telephone" dataField="phone"/> <mx:DataGridColumn headerText="Email" dataField="email"/> </mx:columns> </mx:DataGrid> <mx:Script> <![CDATA[ import mx.utils.ArrayUtil; import tutorials.Person; import mx.collections.ArrayCollection; import mx.rpc.events.ResultEvent; import mx.controls.Alert; import mx.rpc.events.FaultEvent; [Bindable] private var dp:ArrayCollection; [Bindable] private var selectedPerson:Person; private function faultHandler(fault:FaultEvent):void { Alert.show(fault.fault.faultString, fault.fault.faultCode.toString()); } private function getListHandler(evt:ResultEvent):void { dp = new ArrayCollection( ArrayUtil.toArray(evt.result) ); } private function changeHandler(event:Event):void { selectedPerson = Person(DataGrid(event.target).selectedItem); } ]]> </mx:Script> <mx:Button x="290" y="357" label="get list" click="myservice.getOperation('getList').send();"/> <mx:Form x="10" y="174" width="345" height="175"> <mx:FormHeading label="Selected Person" /> <mx:FormItem label="First Name"> <mx:TextInput id="person_first_name" text="{selectedPerson.firstName}" /> </mx:FormItem> <mx:FormItem label="Last Name"> <mx:TextInput id="person_last_name" text="{selectedPerson.lastName}" /> </mx:FormItem> <mx:FormItem label="Telephone"> <mx:TextInput id="person_phone" text="{selectedPerson.phone}" /> </mx:FormItem> <mx:FormItem label="Email"> <mx:TextInput id="person_email" text="{selectedPerson.email}" /> </mx:FormItem> </mx:Form> </mx:Application>
The RemoteObject tag points to the tutorials/PersonService.php class and has 1 declared method within: getList which will invoke the getList method of the PersonService.php file.
The result handler will convert the result given from php to an ArrayCollection and puts the result into the "dp" variable.
A DataGrid component which binds the dataProvider property to the "dp" (ArrayCollection) private variable (in fact the dp variable has the [Bindable] metadata in order to immediately display the results into the datagrid as they come from php ).
Another [Bindable] variable "selectedPerson" which changes when the datagrid selected item changes (see the changeHandler method). the selectedPerson properties (firstname, lastname etc) will be diaplayed into the FormItems.
At least a button which will make the remote call to the getList method using: click="myservice.getOperation('getList').send();"
Remember it's only a beta version of the new amfphp2 stable release and so things could change in the future (hoping in the full support of all great stuff of Flex Data Services 2)..
