2 Replies Latest reply on Dec 26, 2006 1:15 AM by ChrisDut

    "Late" Variables

    ChrisDut Level 1
      I do have a strange thing happening in a small app (my first app, actually).

      I am binding a TextInput (LastName) control with a grid control via a function called FetchContacts() in order to return corresponding records. I do have a CF template returning the XML records as well as a count of the records.

      <mx:HTTPService id="GetContacts" showBusyCursor="true" url= " http://xxxxx.ven.be/tests_cd/Get_Contacts_xml.cfm">

      <mx:TextInput id="LastName_Param" change="FetchContacts();"/>

      <mx:DataGrid id="ContactsGrid" dataProvider="{GetContacts.lastResult.contacts.item}" editable="false" width="800" x="29" height="600" y="87">
      <mx:DataGridColumn headerText="IDCN" dataField="IDCN" />
      <mx:DataGridColumn headerText="FirstName" dataField="FirstName" />
      <mx:DataGridColumn headerText="LastName" dataField="LastName" />
      <mx:DataGridColumn headerText="EMail" dataField="Email" />

      Everything works fine, except that in my function below, adressing GetContacts.lastResult.contacts.contacts_count always gets my the result "one dataset too late" : nothing (an error) on the first request, then the result of the first request on the second one, the second on the third one, etc ...

      public function FetchContacts():void {
      if (GetContacts.lastResult.contacts.contacts_count > 1) {
      Records_label.text = GetContacts.lastResult.contacts.contacts_count + " records";
      } else {
      Records_label.text = GetContacts.lastResult.contacts.contacts_count + " record";

      What do I do wrong ?

      Note : Binding GetContacts.lastResult.contacts.contacts_count to a control works fine - and right on time - but I need to access the variable for other purposes.

      Many thanks in advance for your kind help,
      ... and Merry XMas everybody :-)


        • 1. Re: &quot;Late&quot; Variables
          chris.huston.t10 Level 3
          I think the main problem is that you are treating the HTTPService call as a synchronous event, but it is asynchronous. So, you need to have two functions, one that calls the service and one that listens for the result.

          //The result="some_function(event)" tells your app to listen for the result from the HTTPService call.
          <mx:HTTPService id="GetContacts" result="ShowContacts(event)" showBusyCursor="true" url= " http://xxxxx.ven.be/tests_cd/Get_Contacts_xml.cfm">

          public function FetchContacts():void {

          //This is the function that is triggered when the result comes back from the CF template.
          private function ShowContacts(evt:ResultEvent):void {
          if (evt.result.contacts.contacts_count > 1) {
          Records_label.text = evt.result.contacts.contacts_count + " records";
          } else {
          Records_label.text = evt.result.contacts.contacts_count + " record";

          • 2. Re: &quot;Late&quot; Variables
            ChrisDut Level 1
            Yesss, it works !
            Thanks a lot ... and Merry XMas again :-)