最近在用FLEX4.5开发,学习了Robotlegs框架,记录一下
官方网站下载swc开发包:http://www.robotlegs.org/,下载解压之后找到BIN目录下的robotlegs-framework-v1.5.2.swc拷贝到项目的libs目录下
Robotlegs框架原理图:
大体流程:
AppContext类继承Context类,重写父类的startup()方法:
this.mediatorMap.mapView(UserLogin, UserLoginMediator);
//映射登录mediator,映射之后,UserLogin.mxml组件和UserLoginMediator则绑定在一起了,UserLoginMediator继承Mediator,可重写父类的onRegister()方法通过
this.eventMap.mapListener(userLogin.loginBtn, MouseEvent.CLICK, startUserLogin);
增加对UserLogin.mxml组件的loginBtn按钮的单击事件监听,startUserLogin方法则会通过
dispatch(new UserEvent(UserEvent.LOGIN, u));
派发一个UserEvent.LOGIN的用户自定义LOGIN事件。
this.commandMap.mapEvent(UserEvent.LOGIN, LoginCommand, UserEvent, false);
//通过这句话则告诉LoginCommand让它响应事件,LoginCommand继承Command,可重写父类的execute()方法里面响应事件,这里用到了Inject把
[Inject]
public var event:UserEvent;
用户自定义事件UserEvent注入到LoginCommand类,然后在execute()方法里通过event获得当前的对象,进行业务逻辑处理(可以把Model类也注入进来进行数据模型的一些处理),处理完之后,派发一个自定义事件UserEvent.LOGIN_SUCCESS。
如果UserLoginMediator类里的OnRegister()方法有这句话:
this.addContextListener(UserEvent.LOGIN_SUCCESS, successLogin);
则UserLoginMediator会响应这个自定义事件,然后告诉用户登录成功失败等等。
this.injector.mapSingleton(UserLoginModel); //映射Model类
this.injector.mapSingleton(AppService); //映射Service类(可注入到Meditor)