Flex/ActionScript

From AMI@Work Communities Wiki

This is a simple example on how to receive data from BSCW by using a webservice. We call the GetFolderContent webservice and list the received content in a Data Grid. In the following we describe the function of the single codeblocks.


Contents

[edit] Webservice

The <mx:WebService> MXML-tag gives us access to the operations of SOAP-compliant web services. We call it BSCW, define the location of the WSDL and include an fault event handler. Further we define the operation GetFolderContent, its arguments in XML and its result event handler handleFolderContent.

 <mx:WebService id="BSCW"
   wsdl="http://www.cwe-projects.eu/bscw_resources/wsdl/SharedWorkspaces.wsdl"
   fault="Alert.show(event.fault.message, 'Error')">
   <mx:operation name="GetFolderContent" resultFormat="e4x" result="handleFolderContent(event)">
     <mx:request>
       <request>
         <folderId>6</folderId>
         <depth>1</depth>
         <nested>false</nested>
       </request>
     </mx:request>
   </mx:operation>
 </mx:WebService>

[edit] Data Grid

The Data Grid is a standard component. It lists the content from a data provider. In our example the object folderContent has three columns (name, id and class).

 <mx:DataGrid dataProvider="{folderContent}" left="10" right="10" top="10" bottom="10">
   <mx:columns>
     <mx:DataGridColumn headerText="Name" dataField="name"/>
     <mx:DataGridColumn headerText="ID" dataField="id"/>
     <mx:DataGridColumn headerText="Class" dataField="__class__"/>
   </mx:columns>
 </mx:DataGrid>

[edit] Action Script

In the beginning we have to import some packages and define the variables. The handleFolderContent function is the event handler from the BSCW.GetFolderContent webservice. It only takes a part of the result and saves it in the folderContent object.

 <mx:Script>
   <![CDATA[
     import mx.rpc.events.ResultEvent;
     import mx.controls.Alert;
 
     [Bindable]
     private var folderContent:Object = new Object();
 
     private function handleFolderContent(event:ResultEvent):void
     {
       folderContent = event.result.folderContent.folderContent;
     }
   ]]>
 </mx:Script>

[edit] Complete example

If we compile and run this example it immediately handles the creationComplete event and executes BSCW.GetFolderContent.send(), i.e. calls the webservice. If this asynchronous call sends back a result, the event handler result calls the function handleFolderContent(event). This copies the data in an object, which is bind to a data grid.

 <?xml version="1.0" encoding="utf-8"?>
 <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
   creationComplete="BSCW.GetFolderContent.send()" >
 
 <mx:Script>
   <![CDATA[
     import mx.rpc.events.ResultEvent;
     import mx.controls.Alert;
 
     [Bindable]
     private var folderContent:Object = new Object();
 
     private function handleFolderContent(event:ResultEvent):void
     {
       folderContent = event.result.folderContent.folderContent;
     }
   ]]>
 </mx:Script>
 
 <mx:WebService id="BSCW"
   wsdl="http://www.cwe-projects.eu/bscw_resources/wsdl/SharedWorkspaces.wsdl"
   fault="Alert.show(event.fault.message, 'Error')">
   <mx:operation name="GetFolderContent" resultFormat="e4x" result="handleFolderContent(event)">
     <mx:request>
       <request>
         <folderId>6</folderId>
         <depth>1</depth>
         <nested>false</nested>
       </request>
     </mx:request>
   </mx:operation>
 </mx:WebService>
 
 <mx:DataGrid dataProvider="{folderContent}" left="10" right="10" top="10" bottom="10">
   <mx:columns>
     <mx:DataGridColumn headerText="Name" dataField="name"/>
     <mx:DataGridColumn headerText="ID" dataField="id"/>
     <mx:DataGridColumn headerText="Class" dataField="__class__"/>
   </mx:columns>
 </mx:DataGrid>
 
 </mx:Application>

[edit] See also

Personal tools
community tools