6 Replies Latest reply on Oct 2, 2006 12:40 PM by Mike Kelp

    How to retrive data?

    kruse Level 1
      Hi I'm new to Flex. Im trying to connect to a component in Coldfusion that returns a query.

      How can I retrieve the data from Flex?

      I have tried to use <mx:WebService ..> but builder does not seam to know that command.

      Any one that can help a newb in Flex?
        • 1. Re: How to retrive data?
          Mike Kelp
          Hi, I thought I'd see if I can't help you get on the right track.

          Check out the sample code below. If it gives you an error when it tries to call the service, than I'll need to more about the dev environment you are working in. Basically, when using the ColdFusion destination, the server needs the path to the cfc and the remote methods you want to call. Keep in mind that the swf will assume that the server you want to contact is the server it was downloaded from.

          Hope this helps...

          Example Code:
          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute"
          creationComplete="setup();">
          <!--
          This defines the ColdFusion service object and the methods of it
          that you wish to call. Note that source is the component path to the CFC
          to be called from the webroot of the site.
          fault - I have made this popup an alert box with the error message
          when something goes wrong.
          -->
          <mx:RemoteObject id="productService"
          destination="ColdFusion" source="example.cfcpath.ProductService"
          showBusyCursor="true" fault="Alert.show(event.fault.message, 'Error');">
          <!--
          name - should match the method name being called on the server
          result - is the callback when the remote method has returned.
          Note that we get and event object with all the data involved with the call
          so we can send that to a function to handle it.
          Make sure these methods have access="remote" set in the CFC
          -->
          <mx:method name="getProductList" result="receiveProductList(event);"/>
          </mx:RemoteObject>

          <mx:Script>
          <![CDATA[
          import mx.rpc.events.ResultEvent;
          import mx.collections.ArrayCollection;
          import mx.controls.Alert;

          /*
          * Application properties
          */
          [Bindable] private var productListDP:ArrayCollection = null;

          /*
          * Application Setup
          * Note that this is called in the application tag (creationComplete attribute)
          */
          private function setup():void {
          productService.getProductList();
          }

          /*
          * This function is called when the product list service returns.
          * It receives the data sent by CF in the result property of the event object.
          * We cast this to an ArrayCollection so that it can be used as the grid's dataprovider.
          */
          private function receiveProductList(event:ResultEvent) : void {
          productListDP = event.result as ArrayCollection;
          }

          /*
          * We'll call this function when we want to reload the data again.
          */
          private function reloadProductList() : void {
          // Clear the grid so we can see it updating
          productListDP = null;
          // Call our service again
          productService.getProductList();
          }
          ]]>
          </mx:Script>

          <!-- This button will reload the data -->
          <mx:Button label="Reload Grid" click="reloadProductList();" x="338" y="10"/>

          <!--
          Note that with the dataProvider bound to productListDP,
          it automatically gets updated when changed.
          -->
          <mx:DataGrid id="productList" dataProvider="{productListDP}" x="10" y="40" width="419" rowCount="7">
          <mx:columns>
          <mx:DataGridColumn headerText="Product Title" dataField="title"/>
          </mx:columns>
          </mx:DataGrid>

          </mx:Application>
          • 2. Re: How to retrive data?
            kruse Level 1
            I get an error when running the sample code.

            Unknown destination Coldfusion.

            Do I have to map a variable to the name Coldfusion?

            • 3. Re: How to retrive data?
              Mike Kelp Level 1
              Just to make sure, be sure you are entering "ColdFusion" with a capital F. It does not need to be a variable, mapping, etc. The way it works is the destinations are described in C:\CFusionMX7\wwwroot\WEB-INF\flex\services-config.xml in my case (just adjust it for your ColdFusion install directory). The "ColdFusion" destination is created by default so that is what I used in the example.

              Let me know if it still doesn't work after this information.

              Mike.
              • 4. Re: How to retrive data?
                kruse Level 1
                When I look in my C:\CFusionMX7\wwwroot\WEB-INF\flex\services-config.xml
                I se no ColdFusion.
                I have tried to use the my-cfamf insted but I get the same error.

                <?xml version="1.0" encoding="UTF-8"?>
                <services-config>

                <services>
                <service id="coldfusion-flashremoting-service"
                class="flex.messaging.services.RemotingService"
                messageTypes="flex.messaging.messages.RemotingMessage">

                <adapters>
                <adapter-definition id="cf-object" class="coldfusion.flash.messaging.ColdFusionAdapter" default="true"/>
                </adapters>

                <destination id="ColdFusion">
                <channels>
                <channel ref="my-cfamf"/>
                </channels>
                <properties>
                <source>*</source>
                <!-- define the resolution rules and access level of the cfc being invoked -->
                <access>
                <!-- Use the ColdFusion mappings to find CFCs, by default only CFC files under your webroot can be found. -->
                <use-mappings>false</use-mappings>
                <!-- allow "public and remote" or just "remote" methods to be invoked -->
                <method-access-level>remote</method-access-level>
                </access>

                <property-case>
                <!-- cfc property names -->
                <force-cfc-lowercase>false</force-cfc-lowercase>
                <!-- Query column names -->
                <force-query-lowercase>false</force-query-lowercase>
                <!-- struct keys -->
                <force-struct-lowercase>false</force-struct-lowercase>
                </property-case>
                </properties>
                </destination>

                </service>
                </services>

                <channels>
                <channel-definition id="my-cfamf" class="mx.messaging.channels.AMFChannel">
                <endpoint uri=" http://{server.name}:{server.port}{context.root}/flex2gateway/" class="flex.messaging.endpoints.AMFEndpoint"/>
                <properties>
                <polling-enabled>false</polling-enabled>
                <serialization>
                <instantiate-types>false</instantiate-types>
                </serialization>
                </properties>
                </channel-definition>
                </channels>

                <logging>
                <target class="flex.messaging.log.ConsoleTarget" level="Error">
                <properties>
                <prefix>[Flex] </prefix>
                <includeDate>false</includeDate>
                <includeTime>false</includeTime>
                <includeLevel>false</includeLevel>
                <includeCategory>false</includeCategory>
                </properties>
                <filters>
                <pattern>Endpoint.*</pattern>
                <pattern>Service.*</pattern>
                <pattern>Configuration</pattern>
                <pattern>Message.*</pattern>
                </filters>
                </target>
                </logging>

                <system>
                </system>

                </services-config>
                • 5. Re: How to retrive data?
                  kruse Level 1
                  I found out what the problem was.

                  I right clicked the project folder in the flexbuilder and chose properties. Then I did add this line to the flex compiler arguments

                  -services "C:\CFusionMX7\wwwroot\WEB-INF\flex\services-config.xml" -locale en_US


                  Thak you for your help PerpetualBurn!
                  I wil use Flex alot more now
                  • 6. Re: How to retrive data?
                    Mike Kelp Level 1
                    Glad you got up and running! Sorry I was a little slow responding to your last problem, but I think you'll have a lot of fun now that you are set up. I know I have.

                    Good Luck!