4 Replies Latest reply on Nov 4, 2009 10:25 AM by Subeesh Arakkan

    Displaying dynamic data on load

    rmorgan Level 1

      Hello,

       

      Being a newb, I am trying to figure all this out. I am trying to figure out how to make my dynamic data display. I am close but it still is not working the way it needs to. As you can see in the code I am trying to create a dynamic radio group, to load data when the page loads, but as it is it loads one radio button with a lable of "[object Operation]" and it should be creating 6 radio buttons. You can also see where I have commented out a button, now, if I uncomment it and run the page, pressing the button will populate the radio controls. What I need to do is get it so that when the page loads the data will load without having to press a button. This is taken from examples on the web and modifying to fit my needs. TIA.

       

       

       

       

      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" width="554" height="400">
      <mx:Script>
            <![CDATA[
                  import mx.controls.Alert;
                  import mx.rpc.events.FaultEvent;
                  import mx.rpc.Fault;
                  import mx.rpc.events.ResultEvent;
                  import mx.controls.RadioButton;
                  private function result(evt:ResultEvent):void { 
                        productsRepeater.dataProvider = evt.result;
                 }
                  private function fault(evt:FaultEvent):void {
                        Alert.show(evt.fault.message);                
                 }
            ]]>
      </mx:Script>

       

      <mx:RemoteObject id="mycfc" destination="ColdFusion" showBusyCursor="true" source="flexdemo.src.DemoCFC" result="result(event)" fault="fault(event)">

      </mx:RemoteObject>    
             <!--  <mx:Button label="Button" click="mycfc.GetData()"/> -->
           
            <mx:HRule/>
           
            <mx:Repeater id="productsRepeater" dataProvider="{mycfc}">
            <mx:RadioButton id="buttonsArray"
                label="{productsRepeater.currentItem.affiliatetype}"
                data="{productsRepeater.currentItem}"/>
          </mx:Repeater>

       

      </mx:Application>

        • 1. Re: Displaying dynamic data on load
          Subeesh Arakkan Level 4

          Hi,

           

          Call mycfc.GetData() on applicationComplete event of the application

           

          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" width="554" height="400"

               applicationComplete="mycfc.GetData()">

          • 2. Re: Displaying dynamic data on load
            David A. Tucker

            You are getting close to having this correct - there just needs to be a few changes.

             

            First, the reason that you are getting the [Operation operation] is that you have the dataProvider for the repeater bound to the RemoteObject.  In this case, what you want to do is bind it to the data the RemoteObject returns.  For this example, the first thing you need to do is create a variable that will serve as the dataProvider.  For example, if you have an ArrayCollection (not sure what format your data will be in), our <mx:Script> block would change to be as follows:

             

             

            <mx:Script>
                  <![CDATA[
                        import mx.controls.Alert;
                        import mx.rpc.events.FaultEvent;
                        import mx.rpc.Fault;
                        import mx.rpc.events.ResultEvent;
                        import mx.controls.RadioButton;

             

                        [Bindable]

                        public var returnedData:ArrayCollection;


                        private function result(evt:ResultEvent):void { 
                           // This assumes that an array is being returned by the service

                           // You first tell Flex that this is an array and then use that to create a new ArrayCollection

                           returnedData = new ArrayCollection( evt.result as Array ); 

                           returnedData = evt.result;
                       }
                        private function fault(evt:FaultEvent):void {
                              Alert.show(evt.fault.message);                
                       }
                  ]]>
            </mx:Script>

             

            With that in place, you can now bind the repeater to the correct data.

             

            <mx:Repeater id="productsRepeater" dataProvider="{ returnedData }">

             

            Finally, if you want this to happen when the application loads, you need to listen for the creationComplete method of the application.  When this event is dispatched, you want to tell your remote object to go and grab the data.  Now your opening <mx:Application> tag will look like this:

             

             

             

            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" width="554" height="400" creationComplete="mycfc.getData()">

             

            With those items in place, it should work.

            • 3. Re: Displaying dynamic data on load
              Subeesh Arakkan Level 4

              Yes...I should have checked the entire code.

              • 4. Re: Displaying dynamic data on load
                rmorgan Level 1

                Ok, thanks. Just one more thing I thought of before I give this a shot. On the creationComplete="mycfc.getData()" property inside the application tag, I am questioning if I really want it to work this way. My thought process is telling me no now that I think about it. I am going to be running several different queries as this progresses and it seems that this will end up being broken later down the road.

                 

                For instance,

                On the first page, which we are currently discussing, when the page loads it will display six radio options obtained from a DB for the user to select one of them.  Then when they go to the second page, they will be presented with more options from a DB depending on the selection of the first page. I cant exactly explain and put into words what I am thinking, but if I use this creationComplete="mycfc.getData() in the application tag it would be more of a "global" thing instead of a "local" thing, and would in essence be retrieving the same data over and over as the user progresses through the application.

                 

                I have found that this also works <mx:Repeater id="affiliatesRepeater" dataProvider="{mycfc.GetData()}"> and seems like a better way in this case because it is the control that is asking for the data, and not the application.

                 

                Am I on the right track with my thinking?