Enabling RemoteObject Calls to a ColdFusion Server


When using RemoteObject to communicate with a server in an application served by the FDS, the RemoteObject is inherently looking to communicate back to the FDS server. If you followed the setup directions in the appendix, "Setup Instructions," you have installed the FDS server and the ColdFusion server to run as two separate servers. This setup can greatly simplify configuration issues with the servers, but it unfortunately requires a bit of extra work to enable the application to retrieve data from the ColdFusion server instead of the FDS server. Normally, Flex applications served by the FDS server will use channels as defined in the server's services-config.xml file. This file lacks the necessary information to connect to your ColdFusion server, so you can dynamically add a custom channel to your <mx:RemoteObject> instance instead. Here, you can see a sample of the ActionScript 3.0 code to create a custom channel:

// Create a ChannelSet. private var cs:ChannelSet = new ChannelSet(); // Create a Channel. var customChannel:Channel = new AMFChannel("newAMF", URLtoGateway); // Add the Channel to the ChannelSet. cs.addChannel(customChannel); // Assign the ChannelSet to a RemoteObject instance. svc.channelSet = cs; ... <mx:RemoteObject  ... /> 


To create a new channel, you first need to create an instance of the mx.messaging.ChannelSet class. A ChannelSet is a group of channels that are used to tell RemoteObject calls how to find their destination. Each ChannelSet can contain one or more channels (instances of classes in the mx.messaging.Channel package). In the example code, a new channel called customChannel is defined as a new AMFChannel; that is, a channel that would communicate over the Action Message Format (AMF) protocol. The constructor takes two arguments: a name for the channel and a URL to the Remoting Gateway. If you want to create a channel to talk to your local ColdFusion server, you can replace the URLtoGateway reference with http://localhost:8300/flex2gateway/. The customChannel is then added to the ChannelSet by using the ChannelSet's addChannel() method. Finally, the <mx:RemoteObject> tag has its channelSet specified with the newly created ChannelSet.

In this next exercise, you will create a custom channel to allow the EComm application to continue to leverage the ColdFusion server, even though it is running through your FDS server.

1.

Open Checkout.mxml from the views/ecomm directory of your newly created flexGrocer directory.

Be sure to open the file located in the FDS root because the version of the file you have in the old flexGrocer directory does not need to have it specified.

2.

Create a new private property within the script block in Checkout.mxml named cs of type ChannelSet, which contains an instance of a new ChannelSet.

private var cs:ChannelSet = new ChannelSet(); 


If you used the code-completion features of Flex Builder, the ChannelSet class was automatically imported for you; otherwise, you need to manually add the following:

import mx.messaging.ChannelSet; 


3.

Create a new method called initApp(), which returns void. As the first line of this method, create a local variable named customChannel of datatype Channel, which contains a new AMFChannel with an ID of my-cfamf and a destination of http://localhost:8300/flex2gateway/.

var customChannel:Channel = new AMFChannel("my-cfamf", "http://localhost:8300/flex2gateway/"); 


If the imports for both the Channel and AMFChannel classes were not automatically imported for you, you need to manually add those imports to your file.

import mx.messaging.Channel; import mx.messaging.channels.AMFChannel; 


4.

Still in the initApp() method, add the new customChannel variable to your cs ChannelSet, using the addChannel() method.

cs.addChannel(customChannel); 


By adding the new AMFChannel to the ChannelSet, any elements using this ChannelSet now know how to communicate with your ColdFusion server.

5.

Still in the initApp() method, set the channelSet property of the RemoteObject named svc to be cs.

svc.channelSet = cs; 


This should complete the loop and inform your RemoteObject how to communicate with your separate ColdFusion server.

6.

Add a creationComplete event handler to the root <mx:VBox> tag for the Checkout component. Specify initApp() as the method to be called when the creationComplete Event occurs.

<mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"    xmlns:v="views.ecomm.*"    creationComplete="initApp()"> 


If you save and test your FDS version of the EComm application, it should continue to run as it did in previous lessons. An order number will be sent back upon order completion because it again knows how to communicate with the ColdFusion server.




Adobe Flex 2.Training from the Source
Adobe Flex 2: Training from the Source
ISBN: 032142316X
EAN: 2147483647
Year: 2006
Pages: 225

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net