To specify a CFC to connect to, you do one of the following:
- Specify the CFC, including the path from the web root, in the MXML.
- Create a named resource for each CFC that you connect to. This is similar to registering a data source.
Specify the CFC in the MXML
Specify the CFC, including the path from the web root, in the MXML; for example:
<mx:RemotObject
id="myCfc"
destination="ColdFusion"
source="myApplication.components.User"/>
The destination "ColdFusion" is preconfigured in the flex-enterprise-services.xml with the wildcard * as the source. To use the source attribute in MXML, you can use any destination by specifying the source="*" in flex-enterprise-services.xml. If you specify a source other that "*" in flex-enterprise-services.xml, that source definition overrides the source specified in the MXML.
Create a named resource for each CFC that you connect to
- Edit the WEB-INF\flex\flex-enterprise-services.xml file by adding an entry for each CFC that you connect to, for example:
<destination id="CustomID">
<channels>
<channel ref="my-cfamf"/>
</channels>
<properties>
<source>dot_ path_to_CFC</source>
<lowercase-keys>true</lowercase-keys>
</properties>
</destination>
The source attribute specifies the dot notation to the CFC from the web root (the classpath to the CFC).
- Restart the ColdFusion server.
Use the CFC resource in your Flex Builder 2 project
- For each Flex Builder 2 project, set the Flex compiler property by doing the following:
- Select Project > Properties.
- Select the Flex complier option.
- Enter the following in the Additional Compiler Argument text box:
--services=C:\CFusion\wwwroot\WEB-INF\flex\flex-enterprise-services.xml
- In the mxml file, you use the <mx:RemoteObject> tag to connect to your CFC named resource. With this connection you can call any remote method on the CFC.
- Use the destination attribute of the <mx:RemoteObbject> tag to point to the name that you defined in the flex-enterprise-services.xml file; for example:
<mx:RemoteObject
id="a_named_reference_to_use_in_mxml"
destination="CustomID"
result="my_CFC_handler(event)"/>
- Call a CFC method, for example, as the following example shows:
<mx:Button label="reload" click="my_CFC.getUsers()"/>
In this example, when a user presses a button, the Click event calls the CFC method getUsers.
- Specify a handler for the return result of the CFC method call for the <mx:RemoteObject> tag, as the following example shows.
private function my_CFC_handler( event:ResultEvent )
{
// Show alert with the value that is returned from the CFC.
mx.controls.Alert.show(ObjectUtil.toString(event.result));
}