3 Replies Latest reply on Oct 22, 2007 5:19 PM by ntsiii

    Simple Code Question

    BigBaldGuy
      I'm confused. I'm researching flex for my company. I'm the owner and I'm a database guy. I'm not a programer. below are two grids. The first one uses the first two lines of the mx:WebService. The second one needs all three lines of the mx:WebService and the entire mx:Script block.

      Here is the Question... Why in the world would you code with the mx:Script block. It seems like a waste of energy. Can some one give me the pro's and cons.

      Keep in mind as a database guy I'm in LOVE with ColdFusion.

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical">

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

      [Bindable]
      private var myData:ArrayCollection;
      private function resultHandler(event:ResultEvent):void {
      myData = event.result as ArrayCollection;
      }
      ]]>
      </mx:Script>

      <mx:WebService id="myService"
      wsdl=" http://localhost:8500/flex2/MyService.cfc?wsdl"
      load="myService.getData();" result="resultHandler(event)"/>

      <mx:DataGrid dataProvider="{myService.getData.lastResult}"/>
      <mx:DataGrid dataProvider="{myData}"/>

      </mx:Application>
        • 1. Re: Simple Code Question
          GarthDB
          If you are not going to do anything with the data besides displaying it in the data grid then you would be fine to use the slimmer code. However to use the data for anything else, or to be more scalable in the future, you would be better to use the script block as it would allow you to manipulate the data or to make it available to other components.
          • 2. Re: Simple Code Question
            Gregory Lafrance Level 6
            In mxml files for components that are added to your application mxml file, the script block can define much of the functionallity the component provides and / or needs. The script block can be used in the application mxml file for initialization and massaging the initial state of the application and its data.
            • 3. Re: Simple Code Question
              ntsiii Level 3
              The question is not really about the script block, it is about binding directly to lastResult vs using a result handler.

              As Garth says if you do not need to do anyting with the data before you display it, then direct binding is ok. But such a situation is rare. Before you know it you will be asking something like, "I need to set my datagrid to the same row it was on before I refreshed my data"... anything like this will require a result handler.

              Furthermore, since your DG clearly worked, you were lucky. but what if it did not display correctly the first time you tried it? Direct binding is very hard to debug, since you can't get "between" it an the dg display. Using a result handler, you can inspect/debug the result data in order to craft the dataProvider declaration you want.

              I advise getting into the habit of using a result handler. It will pay off in the long run, if there is a long run for you.

              Tracy