探讨Parsley框架下Controller更新View的方法,总结如下,摘录示例代码,备忘。
1. Message方法,不推荐
1.> controller中
[MessageDispatcher]
public var dispatcher:Function;
dispatcher(new LoginSuccessMessage());
2.> view中
[MessageHandler]
public function LoginSuccessHandle(LoginSuccessMessage):void {
}
2. ChangeWatcher方法,推荐
import mx.binding.utils.ChangeWatcher;
[Inject]
[Bindable]
public var model:LoginModel;
private function viewActiveHandler(event:ViewNavigatorEvent):void
{
ChangeWatcher.watch(model, "LoginStatus", function():void{
if(model.LoginStatus== "loginOK"){
}
else if(model.LoginStatus== "loginFail"){
}
});
}
3.1. 方法1
3.1.1 view中
import mx.binding.utils.BindingUtils;
[Inject]
[Bindable]
public var model:LoginModel;
private function viewActiveHandler(event:ViewNavigatorEvent):void
{
BindingUtils.bindProperty(this,"currentState",model,"LoginStatus");
}
private function LoginOKHandle():void{
// TODO Auto Generated method stub
}
private function LoginFailHandle():void
{
// TODO Auto Generated method stub
}
<s:states>
<s:State name="nologin" />
<s:State name="loginOK" enterState="LoginOKHandle()" />
<s:State name="loginFail" enterState="LoginFailHandle()" />
</s:states>
3.1.2 controller中
[Inject]
[Bindable]
public var model:LoginModel;
model.LoginStatus = "loginOK";
3.2. 方法2
3.2.1 view中
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:parsley="http://www.spicefactory.org/parsley"
currentState="{model.LoginStatus}"
>
[Inject]
[Bindable]
public var model:LoginModel;
{
// TODO Auto Generated method stub
}
private function LoginFailHandle():void
{
// TODO Auto Generated method stub
}
<s:states>
<s:State name="nologin" />
<s:State name="loginOK" enterState="LoginOKHandle()" />
<s:State name="loginFail" enterState="LoginFailHandle()" />
</s:states>
3.2.2 controller中
[Inject]
[Bindable]
public var model:LoginModel;
model.LoginStatus = "loginOK";