Thanks a lot Tim!

 

I didn’t get your code working 100%, only 99% :-)

 

What I had to do was, in Login.as,  in fcnAccessResponse, I used this.dispatchEvent(…) , same as in your code.

But what I had to do was using the loginDelegate.dispatchEvent() method, so it always took the same reference to the class.

 

What I’m thinking is that if you call the dispatchEvent from inside another event, it loses is scope, and “this” is not the class that login.mxml is listening to.

 

I guess that’s the point of singleton classes and that getInstance() method? I should do some research on that, it’s very interesting.

 

Thanks again, it helped me out!


Jonas

 


From: flexcoders@yahoogroups.com [mailto:flexcoders@yahoogroups.com] On Behalf Of Tim Hoff
Sent: dinsdag 2 mei 2006 21:13
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Re: Dispatching an event from inside a resultEvent

 

Hi Jonas,

I took Jeff Tapper's dataManager and revised it to be a little simpler.  I made the onResult event generic "result", integrated the dataManagerEvent code, added a showBusyCursor parameter and renamed it dataServices.  There are better ways to do this, but until final release this is what I'm going with to get data.

There is sample code below.  Perhaps your problem is that Login.as is not a singleton.  I'm not certain, but the samples below are working for me.  Hopefully, you can adapt them to work for you.

Good luck,

Tim Hoff

//*******************************************************

     dataServices.as (formerly dataManager)

//*******************************************************

package org.ets.main.code.business {
 
 import mx.managers.CursorManager;
    import flash.events.EventDispatcher;
    import mx.rpc.soap.WebService;
    import mx.rpc.events.ResultEvent;
    import mx.rpc.events.FaultEvent;
    import mx.rpc.AbstractOperation;
    import mx.controls.Alert;
    import flash.util.*;

    public class DataServices extends EventDispatcher {
        private var ws:WebService;
        private static var instanceMap:Object = new Object();
        public function DataServices(pri:PrivateClass, wsdl:String)
        {
            this.ws = new WebService();
            ws.wsdl = wsdl;
            ws.loadWSDL();
            ws.useProxy = false;
        }
        public static function getDataService(wsdl:String):DataServices
        {
            if(DataServices.instanceMap[wsdl] == null)
            {
             DataServices.instanceMap[wsdl] = new DataServices(new PrivateClass(),wsdl);
         }
         
         var ds:DataServices = DataServices.instanceMap[wsdl];
            if(ds.ws.canLoadWSDL())
            {
                return ds;
            } else {
                throw new Error("BAD WSDL:"+wsdl);
            }
        }
        public function makeRemoteCall(methodName:String,showBusyCursor:Boolean,args:Object):void
        {
            var op:mx.rpc.AbstractOperation = ws[methodName];
            if(showBusyCursor)
            {
             CursorManager.setBusyCursor();
            }
            ws.addEventListener("result",onResult);
            ws.addEventListener("fault",onFault);
            if(args)
            {   
     op.arguments = args;
    }   
    op.send();
    
        }
        private function onResult(result:ResultEvent):void
        {
         CursorManager.removeBusyCursor();
            this.dispatchEvent(result);
        }
        private function onFault(fault:FaultEvent):void
        {
         CursorManager.removeBusyCursor();
            this.dispatchEvent(fault);
        }
        public override function toString():String
        {
            return "DataServices";
        }
    }
}

/**  PrivateClass is used to make DataServices constructor private */
class PrivateClass
{
    public function PrivateClass() {}
}

//*******************************************************

     LoginDelegate.as  (Login.as)

//*******************************************************

package org.ets.main.code.business
{
 

import flash.events.EventDispatcher;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;

import org.ets.main.code.business.LoginDelegate;  

import org.ets.main.code.business.DataServices;

 

 public class LoginDelegate extends EventDispatcher
 {
  private static var loginDelegate:org.ets.main.code.business.LoginDelegate;
  private var dataServices:DataServices;
  
  public static function getInstance() : org.ets.main.code.business.LoginDelegate
  {
   if ( loginDelegate == null )
    loginDelegate = new org.ets.main.code.business.LoginDelegate();
    
   return loginDelegate;
    }

 //------------------------------------------------------------
 
  public function performLogin( strUserName:String, strPassWord:String ): void
  {    
   var showBusyCursor : Boolean = true
   var args:Object = new Object();
   args = [strUserName,strPassWord];
   
   dataServices = DataServices.getDataService(Wsdl.loginWSDL);
   dataServices.addEventListener("result",onResult);
   dataServices.addEventListener("fault",onFault);
   dataServices.makeRemoteCall("get_access",showBusyCursor,args);

   args = null;
  } 
  
  //----------------------------------------------------------------------------

  private function onResult(event:ResultEvent):void
  { 
            this.dispatchEvent(event);
            dataServices.removeEventListener("result",onResult);
            dataServices.removeEventListener("fault",onFault);        
        }

//----------------------------------------------------------------------------

  private function onFault(event:FaultEvent):void
  {   
            this.dispatchEvent(event);
            dataServices.removeEventListener("result",onResult);
            dataServices.removeEventListener("fault",onFault);
        }
  }

//----------------------------------------------------------------------------

}

 

//*******************************************************

     Login.mxml  (import LoginDelegate.as)

//*******************************************************

import org.ets.main.code.business.LoginDelegate;  

 

public function performLogin():void
  {      
         var loginDelegate : LoginDelegate = LoginDelegate.getInstance();
   
         loginDelegate.addEventListener("result",onResult);
         loginDelegate.addEventListener("fault",onFault);           
         loginDelegate.performLogin( strUserName, strPassWord );
  }
  
//----------------------------------------------------------------------------
 
  public function onResult( event : ResultEvent ) : void
  {    
      // do something
      var loginDelegate. : LoginDelegate = LoginDelegate.getInstance();   
      loginDelegate..removeEventListener("result",onResult);
      loginDelegate..removeEventListener("fault",onFault);
  }
  
//----------------------------------------------------------------------------
 
  public function onFault( event : FaultEvent ) : void
  {
      // do something
      var loginDelegate: LoginDelegate = LoginDelegate.getInstance();   
      loginDelegate.removeEventListener("result",onResult);
      loginDelegate.removeEventListener("fault",onFault);   }
  
//----------------------------------------------------------------------------

 


--- In flexcoders@yahoogroups.com, "Jonas Windey" <[EMAIL PROTECTED]> wrote:
>
> Hi,
>
>
>
> Though question here, and props for the person who can help me here.
>
> I have an mxml file and an as 3.0 class that handles the data. I also use
> the datamanager classes from Jeff Tapper
> (http://jeff.mxdj.com/as3_datamanager.htm) to handle the webservice
> requests.
>
>
>
> Here's some code I use
>
>
>
> In my Login.mxml:
>
>
>
> private function fcnSubmitLogin():void
>
> {
>
> var oLogin:Login = new Login();
>
> oLogin.addEventListener("testEvent",fcnTest);
>
> oLogin.performLogin(txtUserName.text,
> txtPassWord.text);
>
> }
>
> private function testing(event:Event):void
>
> {
>
> Alert.show("test result");
>
> }
>
>
>
>
>
> In Login.as (which extends EventDispatcher)
>
>
>
>
>
> public function init():void
>
> {
>
> loginManager = DataManager.getDataManager(Wsdl.loginWSDL);
>
>
>
> loginManager.addEventListener("get_accessResponse",fcnAccessResponse);
>
>
> }
>
> public function performLogin(strUserName:String,
> strPassWord:String):void
>
> {
>
> loginManager = DataManager.getDataManager(Wsdl.loginWSDL);
>
>
>
> loginManager.makeRemoteCall("get_access","get_accessResponse");
>
>
>
> fcnTest();
>
>
> }
>
> public function
> fcnAccessResponse(event:DataManagerResultEvent):void
>
> {
>
> fcnTest();
>
> }
>
> private function fcnTest():void
>
> {
>
> Alert.show("in fcnTest");
>
> var e:Event = new Event("testEvent ");
>
> this.dispatchEvent(e);
>
> }
>
>
>
> Now, what happens here is the following
>
>
>
> Login.mxml adds the eventlistener so it listens until Login.as broadcasts
> the event "testEvent"
>
> The first time it's working perfectly, the method performLogin calls
> fcnTest(), fcnTest dispatches the event "testEvent", and I get both alerts
> ("in fcnTest" from Login.as, and "test result", from Login.mxml);
>
>
>
> Now comes the problem, after the webservice is called, I get to the method
> fcnAccessResponse (which is called if access is granted)
>
> The method fcnAccessResponse calls fcnTest(), and I get the alert "in
> fcnTest"
>
>
>
> The problem is, fcnTest also dispatches the event "testEvent", but it
> doesn't get noticed by Login.mxml, whereas the first time the method is
> called, it gets triggered.
>
>
>
> Anyone got a clue? Been searching for hours now.
>
>
>
> Thanks!
> Jonas
>




--
Flexcoders Mailing List
FAQ: http://groups.yahoo.com/group/flexcoders/files/flexcodersFAQ.txt
Search Archives: http://www.mail-archive.com/flexcoders%40yahoogroups.com




YAHOO! GROUPS LINKS




Reply via email to