<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>devahead BLOG &#187; BlazeDS</title>
	<atom:link href="http://www.devahead.com/blog/category/blazeds/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.devahead.com/blog</link>
	<description>Adobe Flex, Flash Platform and everything else about software development from a developer&#039;s point of view</description>
	<lastBuildDate>Thu, 26 Jan 2012 23:27:11 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Sending ActionScript objects through BlazeDS with ASObject</title>
		<link>http://www.devahead.com/blog/2010/04/sending-actionscript-objects-through-blazeds-with-asobject/</link>
		<comments>http://www.devahead.com/blog/2010/04/sending-actionscript-objects-through-blazeds-with-asobject/#comments</comments>
		<pubDate>Sun, 18 Apr 2010 23:24:27 +0000</pubDate>
		<dc:creator>Andrea Bresolin</dc:creator>
				<category><![CDATA[BlazeDS]]></category>
		<category><![CDATA[ASObject]]></category>
		<category><![CDATA[Flex]]></category>

		<guid isPermaLink="false">http://www.devahead.com/blog/?p=21</guid>
		<description><![CDATA[Typically, BlazeDS is used to send typed objects between a Flex client and a Java server, but what if you want to send generic objects generated dynamically? On the client side, you can create an ActionScript object dynamically through the Object base class. So, for example, you can write something like this:


var myObject: Object = [...]]]></description>
			<content:encoded><![CDATA[<div class="post-attachments"><span class="source-link"><a href="http://www.devahead.com/blog/examples/BlazeDSASObject/BlazeDSASObject.zip">Source code</a> <strong><a href="about#sourcelicense">(LICENSE)</a></strong></span></div>
<p style="padding-top:10px">Typically, BlazeDS is used to send typed objects between a Flex client and a Java server, but what if you want to <strong>send generic objects generated dynamically</strong>? On the client side, you can create an ActionScript object dynamically through the <strong><em>Object</em></strong> base class. So, for example, you can write something like this:</p>
<div class="post-source-code">
<pre class="brush: as3;">
var myObject: Object = new Object();
myObject.prop1 = &quot;prop1Value&quot;;
myObject.prop2 = new Object();
myObject.prop2.prop21 = &quot;subValue1&quot;;
myObject.prop2.prop22 = &quot;subValue2&quot;;
</pre>
</div>
<p>Just a simple example that shows how to dynamically add custom properties to a generic ActionScript <em>Object</em>. What could not be obvious is how to obtain the same functionality on the server side with Java. In fact, the <em>Object</em> class in ActionScript corresponds to the <strong><em>ASObject</em></strong> class in Java. What is important to note is that <strong>the <em>ASObject</em> class extends <em>java.util.HashMap</em></strong>. How can this information be useful? To understand it, let&#8217;s immediately take a look at our example application.</p>
<div class="post-source-code">
<pre class="brush: as3;">
&lt;mx:Application xmlns:mx=&quot;http://www.adobe.com/2006/mxml&quot;
	pageTitle=&quot;Sending ActionScript objects through BlazeDS with ASObject - devahead BLOG&quot;
	layout=&quot;vertical&quot; horizontalAlign=&quot;left&quot;&gt;

	&lt;mx:Script&gt;
		&lt;![CDATA[
			import mx.rpc.events.FaultEvent;
			import mx.rpc.events.ResultEvent;
			import mx.controls.Alert;
			import mx.rpc.AsyncToken;

			protected static const ENDPOINT: String = &quot;messagebroker/amf&quot;;

			protected function getRemoteObject(): void
			{
				// Create a generic ActionScript object to send to the server
				var sentGenericObj: Object = new Object();
				sentGenericObj.prop1 = 123;
				sentGenericObj.composite = new Object();
				sentGenericObj.composite.sub1 = &quot;subValue1&quot;;
				sentGenericObj.composite.sub2 = 0.57;

				var call: AsyncToken = testService.getSentGenericObject(sentGenericObj);
				call.addResponder(new mx.rpc.Responder(
					onGetRemoteObjectOK, onGetRemoteObjectFault));
			}

			protected function onGetRemoteObjectOK(event: ResultEvent): void
			{
				var result: Object = event.result;

				Alert.show(&quot;Returned object:\n\n&quot; +
					&quot;prop1 = &quot; + result.prop1 + &quot;\n&quot; +
					&quot;composite.sub1 = &quot; + result.composite.sub1 + &quot;\n&quot; +
					&quot;composite.sub2 = &quot; + result.composite.sub2);
			}

			protected function onGetRemoteObjectFault(event: FaultEvent): void
			{
				Alert.show(&quot;Error retrieving remote object.\n\n&quot; +
					&quot;faultString: &quot; + event.fault.faultString + &quot;\n&quot; +
					&quot;faultDetail: &quot; + event.fault.faultDetail + &quot;\n&quot; +
					&quot;faultCode: &quot; + event.fault.faultCode, &quot;Error&quot;);
			}
		]]&gt;
	&lt;/mx:Script&gt;

	&lt;mx:RemoteObject id=&quot;testService&quot; destination=&quot;testService&quot; endpoint=&quot;{ENDPOINT}&quot;
		showBusyCursor=&quot;true&quot;/&gt;

	&lt;mx:Button label=&quot;Get remote object&quot; click=&quot;{getRemoteObject()}&quot;/&gt;

&lt;/mx:Application&gt;
</pre>
</div>
<p>In this application, we call a server function named <em>getSentGenericObject</em>. It&#8217;s purpose is simply to send back to the client an object generated dynamically on the server side whose values are the same of the object received in input from the client. As you can see, we create a generic ActionScript object called <em>sentGenericObj</em>, add some properties and send it to the server, then we print on an alert message the values of the object received from the server. Now it&#8217;s time to see how <em>getSentGenericObject</em> is implemented. <span id="more-21"></span></p>

<div class="post-source-code">
<pre class="brush: java;">
package com.devahead.services;

import flex.messaging.io.amf.ASObject;

public class TestService
{
	@SuppressWarnings(&quot;unchecked&quot;)
	public Object getSentGenericObject(ASObject genericObj)
	{
		// Create a new ASObject instance to send to the client with all
		// the properties values received from the client.
		ASObject returnedObj = new ASObject();

		returnedObj.put(&quot;prop1&quot;, genericObj.get(&quot;prop1&quot;));

		ASObject compositeObj = new ASObject();
		returnedObj.put(&quot;composite&quot;, compositeObj);

		compositeObj.put(&quot;sub1&quot;, ((ASObject)genericObj.get(&quot;composite&quot;)).get(&quot;sub1&quot;));
		compositeObj.put(&quot;sub2&quot;, ((ASObject)genericObj.get(&quot;composite&quot;)).get(&quot;sub2&quot;));

		return returnedObj;
	}
}
</pre>
</div>
<p>Since <em>ASObject</em> is in fact an <em>HashMap</em>, all we need to do to generate dynamically an object in Java is add the properties we need as keys of the <em>HashMap</em> and for each property we can assign a value. It&#8217;s really simple. As you can see, we create a new instance of <em>ASObject</em> called <em>returnedObj</em> and add a property named <em>prop1</em> getting its value from the <em>genericObj</em> received from the client. We also create a composite property called <em>composite</em>. With composite I mean a property with sub-properties in it, so <em>composite</em> is another <em>ASObject</em> instance and we can add the sub-properties as we previously did with <em>prop1</em>. This is all we need to do. In this way, we can get a dynamically generated object both on the client and the server side and send it back and forth through BlazeDS. This is the result we get when we click the button on the example application:</p>

<p><img src="http://www.devahead.com/blog/images/BlazeDSASObject/getRemoteObjectResult.jpg"/></p>
<p>
]]></content:encoded>
			<wfw:commentRss>http://www.devahead.com/blog/2010/04/sending-actionscript-objects-through-blazeds-with-asobject/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

