Flex mate框架学习笔记(二)——Injectors

injectors:依赖注入。借助mate的injectors可以简单的实现MVC。当A触发一事件,EventHandler拦截该事件后,可以去请求服务器或者调用其他的方法来完成业务逻辑,再由
injectors来通知B,这里有点像struts的“转向”。

1、自定义事件类
// ActionScript file
package mymate.event
{
import flash.events.Event;

public class MyEvent extends Event
{

public static const CLICK_ME2 = "click_me2";
//可以是任意类型包括对象。
public var name:String;

public var password:String;

public function MyEvent(type:String,bubbles:Boolean=true,cancelable:Boolean=false):void
{
super(type,bubbles,cancelable);
}
}
}


2、分发事件

private function send2(event:MouseEvent):void
{
var e:MyEvent = new MyEvent(MyEvent.CLICK_ME2);
e.name = "谷正东2";
e.password="123456";
this.dispatchEvent(e);
}
<mx:Button label="点我" click="send2(event)"/>


3、EventMap
<?xml version="1.0" encoding="utf-8"?>
<EventMap xmlns:mx="http://www.adobe.com/2006/mxml" xmlns="http://mate.asfusion.com/">
<mx:Script>
<![CDATA[
import mymate.event.MyEvent;
import mymate.view.A;
import mymate.business.TestMObj;
]]>
</mx:Script>
<EventHandlers type="{MyEvent.CLICK_ME2}">
<MethodInvoker generator="{TestMObj}" method="say" arguments="{event}"/>
</EventHandlers>
<Injectors target="{A}">
<PropertyInjector targetKey="isTruePwd" source="{TestMObj}" sourceKey="message"/>
</Injectors>
</EventMap>


当触发事件后,EventHandlers 根据事件类型扑捉到该事件, 创建MethodInvoker 指定的TestMObj对象,并调用say方法。完成密码校验。
TestMObjct.as
package mymate.business
{
import mymate.event.MyEvent;
import mx.controls.Alert;

public class TestMObj
{
public function TestMObj()
{
}
[Bindable]
public var name:String;
[Bindable]
public var message:String;

public function say(event:MyEvent):void
{
var password:String = event.password;
if(password=="123456")
{
this.message = "您输入密码正确";
}else
{
this.message = "您的密码输入有误";
}
}
}
}



<Injectors target="{A}">
<PropertyInjector targetKey="isTruePwd" source="{TestMObj}" sourceKey="message"/>
</Injectors>


将TestMobject中的message 注入给A中的isTruePwd属性。

A.mxml
	[Bindable]
public var isTruePwd:String="";
<mx:Label text="{isTruePwd+' ***'}"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值