Posts Tagged ‘Bindable’

An AppFuse-like JSON-Flex Example

While I’ve been doing a little research, I actually created two working projects that demonstrate a JSON to Flex mashup. The Flex project will probably evolve into a complicated mess over time because I’ll use it for testing with an AppFuse instance hosted on my local machine. For now, however, I’m going to tag compatible versions so that those that want to take a look at it can get it to work easily.

The two projects are found on Github here: http://github.com/dlwhitehurst/appfuse-rest and http://github.com/dlwhitehurst/appfuse-flex-client First, download and build the rest example (tag v0.1-MASHUP) first. If you don’t see the tag yet, I haven’t done it but you can trust me that the latest commits work. I’ll tag tonight.

Once you successfully build a WAR file you can “mvn jetty:run-war” in a terminal and just leave it running. To see the Flex client in action you can just double-click on the main.html in the $project/bin-debug folder. That’s it! You should see a simple button and a datagrid. Push the button and the datagrid should load.

All of the source code is available in both projects. Check it out. The difficult part was getting the data to display in the datagrid. The Bindable declaration is what fixed my problem. I had read that even if the data (JSON) was parsed and visible as an Array, the data would not work with the mx:DataGrid object unless it was converted to an ArrayCollection object. I fumbled with that for a while then I started getting warnings about static types vs. dynamic components. I eventually found an example where [Bindable] was used just over a variable declaration for the ArrayCollection that would populate the mx:DataGrid and that worked! Here’s the main.mxml that does it all.

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?xml version="1.0" encoding="utf-8"?>
<!-- add license -->
 
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
	backgroundGradientColors="[0xFFFFFF, 0xAAAAAA]"
	horizontalAlign="left"
	verticalGap="15" horizontalGap="15"
	>
 
	<mx:Script>
		<![CDATA[ 
		import mx.rpc.events.ResultEvent;
		import mx.controls.Alert;
                          import com.adobe.serialization.json.*;
		import mx.collections.ArrayCollection;
 
                         [Bindable]
	            private var customersCollection:ArrayCollection;
 
	            private function runService():void
	            {
     	                  getCustomers.send();
	             }
 
                         private function resultHandler(event:ResultEvent):void {
	                  var rawData:String = String(event.result);
     	                  var customers:Array = JSON.decode(rawData) as Array;
     	                  customersCollection = new ArrayCollection(customers);
                         }
 
 
		]]> 
	</mx:Script>
 
	<mx:HTTPService id="getCustomers"
		url="http://localhost:8080/customers.json"
		method="GET"
		resultFormat="text" 
		result="resultHandler(event)">
		<mx:request xmlns="">
      		<getCustomers>"true"</getCustomers>
    	</mx:request> 
	</mx:HTTPService>
 
    <mx:Button x="266" y="338" label="Get Customers" id="getCustomersButton"
         click="runService()"/>
 
	<mx:DataGrid id="cGrid"
		x="80" y="141" 
		width="400" height="92" 
		editable="false" 
		enabled="true"
		dataProvider="{customersCollection}">
	    <mx:columns>
	        <mx:DataGridColumn headerText="Id" dataField="id" />
	        <mx:DataGridColumn headerText="Name" dataField="name" />
	        <mx:DataGridColumn headerText="Phone" dataField="phone" />
	    </mx:columns>
	</mx:DataGrid>
 
</mx:Application>

Search
Categories
Bookmarks