6 Replies Latest reply on Aug 4, 2009 10:18 PM by ice1000

    Custom Components & Webservice Help

    ice1000 Level 1

      I built two custom components, one based on a button and the other on a chart.

      The webservice has been defined with the following tag in the button component:

          <mx:WebService id="ws" wsdl="http://localhost/Service1.asmx?WSDL">
                  <mx:operation name="GetData"/>
          </mx:WebService>

       

      I want to be able to add a listener to the chart component (not the button) that will listen for the result event from webservice and refresh the chart. There will eventually be many buttons and many charts so each chart should subscribe to a particular call's result event.

       

      The strucure of my test application is something like this:

       

      <Application>

        <Button (webservice call in here)>

        <Chart (result event listener in here)>

      </Application>

       

      I'm thinking this should be pretty simple but I can't figure it out. Any help would be greatly appreciated!

       

      Ivan

        • 1. Re: Custom Components & Webservice Help
          Madhav Subedi Level 4

          you need to fire a custom event from button component when result comes,

           

          catch this event in main application and in the eventhandler, refresh the chart component.

          • 2. Re: Custom Components & Webservice Help
            Travis Lazar

            You could also, although not as clean as creating a custom event, direct your operation's result handler to a refresh method within your chart.

             

            Your operation tag would look something like:

             

            <mx:Operation name="getData" result="refreshChart()" />

             

            Your refreshChart() method handles refreshing your chart however you see fit.

             

             

             

             

             

            And yet another option would be to create a [Bindable] data collection of some kind (Array, ArrayCollection, etc) and within your chart bind the dataProvider to this dataCollection, like follows:

             

            <mx:LineChart dataProvider="{myData}" .......>

            </mx:LineChart>

             

            and then your operation would call a resultHandler like:

             

            <mx:Operation name="getName" result="resultHandler(event)" />

             

            within your result handler you would set your data collection to the data received:

             

            private function resultHandler(event:ResultEvent):void {

                 myData = event.result;   (You'll have to do some data formatting here to get it to fit into your array collection correctly)

            }

             

            With the data binding this last option should be pretty simple.

            • 3. Re: Custom Components & Webservice Help
              ice1000 Level 1

              OK I think I understand.

               

              I put the following in the button component:

                  <mx:Metadata>
                      [Event(name="returnData", type="mx.rpc.events.ResultEvent")]
                  </mx:Metadata>

               

              The main application has the button component as:

                              <comp:updateButton
                                   id="updatePie"
                                   returnData="handleResult(event)"/>

               

              The handler in Main:

              private function handleResult():void {

               

              }

               

              How do I reference the chart component & refresh the data in the function?

              • 4. Re: Custom Components & Webservice Help
                Madhav Subedi Level 4

                you would have some arraycollection as the dataprovider for the chart component, just refresh( update ) that.

                • 5. Re: Custom Components & Webservice Help
                  Travis Lazar Level 1

                  Yes if you make a [Bindable] array collection and set that as your dataProvider...

                   

                  [Bindable] var myArray:ArrayCollection = new ArrayCollection();

                   

                  Then set your chart's dataProvider to myArray....

                   

                  <mx:PieChart dataProvider="{myArray}" />

                  </mx:PieChart>

                   

                  Whenever myArray changes, it will automatically update your pie chart. There really is not need to create a custom event here, if you're doing the change on button click you can use the click event that's already built into the button, or the result event that's built into the web service objects (if that's how you're getting your data).

                  • 6. Re: Custom Components & Webservice Help
                    ice1000 Level 1

                    Thanks! Got it working!