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、分發事件
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+' ***'}"/>