3 Replies Latest reply on Aug 10, 2007 3:54 PM by ntsiii

    EventListener based program structure

    jogla
      Hello,

      I have some problems that occur because of the haevyness flex uses events.

      When i let a user input a name, and ask a java servlet for all names that are already taken, to compare them, i have to make a http request (I use HttpService). After that, i get the result event and can compare that name to the result.
      But.. in the method recieving the result event, i don't know the name i asked the user for anymore.

      private function doRequest(name:String, url:String):void {
      var service:MyDefaultHttpService = new MyDefaultHttpService(url);

      service.addEventListener(ResultEvent.RESULT, onResultEvent);

      service.send();
      }

      private function onResultEvent(event:ResultEvent):void {
      // I don't now name anymore.
      }

      Thats a problem i have *very* often. You can make the name global as a workaround.

      But lets assume that you want to make this http call i did very often. And there are serveral ways to process the result.
      Then you have sometimes more than one variable. Maybe 5 or 10 and its would not be very good to make them all global.

      You could argument, that i can make the http call first and then ask the user for input (then name may not be global), but what if have to do more than one http call?
      Then the problem still remains.

      If there are different ways to process the data, there are different methods that want to get the result event.
      What should i do, for example, if i want to use the above method doRequest(), that was written to get a list of names and then compare it to a string that the user enters, when i only want to show the names? I have to use another event result listener.
      I could copy and paste the method that gets the names from the server und have it twice with minor differences, but thats very ugly. I also could store a global var methodToCallOnResult:Function. Thas very ugly too. I could give the function as parameter to doRequest(....., ......, functionToCallOnResult), and event that is very ugly.

      Maybe i'm overlooking something.

      Greetings Jogla

        • 1. Re: EventListener based program structure
          peterent Level 2
          I assume you are making the HTTP request and handling the result in the same class (AS or MXML file). Perhaps the TextInput control is in another class? In this case you could pass in the value of the TextInput control's text property to the class with the data service call and keep it as a class member variable - global to the file only, not to the whole program. Plus you could use data binding to make it more elegant:

          <mx:TextInput id="userName" ... />
          <CheckWithServer id="serverCheck" user="{userName.text}" ... />
          <mx:Button label="Submit" click="serverCheck.go()" />

          Then in CheckWithServer you could have:

          [Bindable] public var user:String;

          public function go() : void {
          // make the request, passing in the value of user
          }

          public function handleResult( event:ResultEvent ) : void {
          // compare the result to user
          }

          • 2. Re: EventListener based program structure
            jogla Level 1
            Hmm..

            sometimes that is not enough.

            What e.g. if i have a "save project/config" http request. After that request is done, i show a Dialog saying "Configuration saved.".

            But.. sometimes a want to save without saying "Configuration saved", for example when doing some kind of "auto save".

            I just can't get "behind" the call. I have to set a flag. And if i want a third way of reacting after a successful call, then a flag is not enough and i have to store the Function that should be called after success. -- And I don't think that this is could code style.

            And if i have code that saves something and *after* the saving is complete should do some things i can not rely on local variables, because after the http call, they are all *gone*. A have to make them class members. And if i have to make all variables class members that I need to survive a http call or dialog window (e.g. for user input) then all my classes will become unreadably and hard to understand.

            Greetings Jogla
            • 3. Re: EventListener based program structure
              ntsiii Level 3
              Are you aware of the AsyncToken class? If not, take a look.

              Tracy