Actionscript 3 Flex正在等待httpservice返回数据

Actionscript 3 Flex正在等待httpservice返回数据,actionscript-3,apache-flex,events,actionscript,flex4,Actionscript 3,Apache Flex,Events,Actionscript,Flex4,真的。我真的被Flex惹恼了。有没有一种方法可以让它在不使用计时器的情况下等待httpservice获取数据 目前,我的代码如下所示: protected function loginUser(event:MouseEvent):void { if(txtEmail.text == "" || txtPassword.text == "") { Alert.show("Please complete all fields!", "Oops!"); }

真的。我真的被Flex惹恼了。有没有一种方法可以让它在不使用计时器的情况下等待httpservice获取数据

目前,我的代码如下所示:

protected function loginUser(event:MouseEvent):void
{
    if(txtEmail.text == "" || txtPassword.text == "")
    {
        Alert.show("Please complete all fields!", "Oops!");
    }
    else
    {
        user = new User(txtEmail.text, txtPassword.text);
        user.login(user);

        var loginTimer:Timer = new Timer(1000, 1);
        loginTimer.addEventListener(TimerEvent.TIMER_COMPLETE, dispatchLoginEvent);
        loginTimer.start()
    }
}
package service.event {
   public Class UserEvent extends Event {
      public static const REQUEST_LOGIN:String = 'requestLogin';
      //you may have other events that apply to users as well

      public var email:String;
      public var password:String;

     public function UserEvent (type:String, email:String, password:String):void {
         //I assume this will be dispatched from a View, given your existing code, so it will bubble by default
         super(tyoe, true, true);
         this.email=email;
         this.password=password;
     }
     override public function clone():Event {
        return new UserEvent(type, email, password);
     }
   }
}
当我执行
user.login()
时,它会从外部AS类发送一个带有HTTPservice的请求。在这个httpservice的结果事件处理程序中,我将一个公共变量设置为true或false,这取决于用户的凭据在数据库中是否正确

然后我使用一个getter来获取那个布尔值。但是,如果没有计时器,它总是返回false,因为我的代码比事件结果处理程序快。如果这有道理的话

因此,我必须使用计时器将我的应用程序暂停一秒钟。。但说真的,这对我来说毫无意义。一定有更好的办法,不是吗


如果有人愿意帮助我,我可以提供更多的代码。谢谢。

正确的模式是在用户对象上使用回调或事件侦听器

protected function loginUser(event:MouseEvent):void
{
    ...

    user = new User(txtEmail.text, txtPassword.text);
    // pass a callback function to the login method 
    user.login(user, function(result:Boolean):void{
            // do something with the result
    });    
}

你得原谅我的动作脚本我有一段时间没做任何动作了。但希望你能明白。在Flex中,您永远不应该设置计时器来轮询对象上的布尔值,答案总是注册一个侦听器或一些描述。

您似乎没有完全理解我对其他问题的回答

//Note the argument should NOT be a Mouse event, because that would be dispatched by a View.
//you should not have this kind of business logic in a View
protected function loginUser(event:UserEvent):void {
   //validate before even trying to create a user, since this
   //has the side effect of running an HTTPService call
   if (event.login != null && event.password != null) {
      user = new User(event.email, event.password);
      //these are plain old events dispatched by your user 
      //Note that strictly speaking a user should not be retrieving itself
      user.addEventListener('loginSuccess', onLoginSuccess);
      user.addEventListener('loginFailed', onLoginFailed);
   } else {
      dispatchEvent(new Event('incompleteUserDetails'));
   }

}

protected function onLoginSuccess(e:Event):void {
   //do your thing here
}

protected function onLoginFailed(e:Event):void {
   //trigger your error handling logic here
}
您的UserEvent类应该如下所示:

protected function loginUser(event:MouseEvent):void
{
    if(txtEmail.text == "" || txtPassword.text == "")
    {
        Alert.show("Please complete all fields!", "Oops!");
    }
    else
    {
        user = new User(txtEmail.text, txtPassword.text);
        user.login(user);

        var loginTimer:Timer = new Timer(1000, 1);
        loginTimer.addEventListener(TimerEvent.TIMER_COMPLETE, dispatchLoginEvent);
        loginTimer.start()
    }
}
package service.event {
   public Class UserEvent extends Event {
      public static const REQUEST_LOGIN:String = 'requestLogin';
      //you may have other events that apply to users as well

      public var email:String;
      public var password:String;

     public function UserEvent (type:String, email:String, password:String):void {
         //I assume this will be dispatched from a View, given your existing code, so it will bubble by default
         super(tyoe, true, true);
         this.email=email;
         this.password=password;
     }
     override public function clone():Event {
        return new UserEvent(type, email, password);
     }
   }
}

您可以根据电子邮件和密码字段的值从视图中发送该消息,并在显示列表的更高位置侦听该消息。

如果您使用的是HttpService对象,我认为正确的方法是在数据到达后,在此HttpService对象上使用“ResultEvent”和“FaultEvent”来处理数据

您可以看到以下关于如何使用HttpService对象的示例,以便在从服务器完全接收数据后触发函数


谢谢。我会设法弄明白的。。不太清楚代码是如何工作的,但这只是一个开始。非常感谢。好的,我将回调函数传递到登录函数中,并让该函数处理结果?问题是,结果来自
loginHandleResult(事件:ResultEvent):void
函数,而不是登录函数本身。。我的Flex风格更倾向于Javascript风格,并传递回调@Amy Blankenship的回答显示了一种更灵活的方法。首先教新手函数式编程可能不是一个好主意。Javascript只存在于HTML页面的生命周期中,因此没有那么多内存泄漏的危险。不过,如果这是一种吸引人的技术,请查看有关该方法优缺点的更深入信息。出于好奇,如果登录失败,你有什么计划?丢弃所有创建的错误用户并重新开始?我不确定:我们必须为UNI做一个Flex应用程序,但是我们几乎没有学到任何东西,这是地狱般的东西。我会仔细检查你的代码并尝试理解它。谢谢你的努力!这可能需要太长的时间让你通过你的课程,但是你可能想考虑去AS3DP.com至少学习一些关于G的基础性东西。在你进入现实世界之前,先做ood建筑。现实是,很少有人以此为生有时间或倾向于教书。谢谢。现实是,我们的老师不太聪明,对最佳实践一窍不通。基本上,我们只能靠自己。我在这里仍然坚持计时,但我怀疑她不会注意。一旦我完成了应用程序的其他部分,我会尝试找出答案。感谢您的帮助,我一定会检查您的代码和您提供的链接!只要老师也在您的机器上运行代码,这种方法应该可以完美地工作:-p。但我不想尝试预测一个对您有效的计时器长度请注意,除了我对您的答复之外,如果您的布尔变量可以与普通元标记绑定,那么您也可以侦听它的属性更改事件。