swiz使用mediate元数据标签处理事件。
当你设置配置文件swizconfig中的mediateBubbledEvents为true,你的视图可以简单派发一个事件,该事件将被swiz的DynamicMediator捕获。包含Mediate的这个类必须是一个定义在BeanLoader的类或视图。
.简单事件
.事件类
.优先权
.
.控制事件流
.可撤消事件工作原理
.非视图类派发事件
简单事件
// 事件必须设置bubbles为true
dispatchEvent(new Event("fooEvent", true))
控制代码
[Mediate(event="fooEvent")]
public function fooHandler():void{//...}
事件类
//事件类
package example.event
{
import flash.events.Event;
public class FooEvent extends Event
{
public static const FOO:String = "foo";
public var someValue:String;
public function FooEvent(type:String, someValue:String, bubbles:Boolean=true, cancelable:Boolean=false)
{
super(type, bubbles, cancelable);
this.someValue = someValue;
}
override public function clone() : Event
{
return new FooEvent(type, someValue, bubbles, cancelable);
}
}
}
视图代码
dispatchEvent(new FooEvent(FooEvent.FOO, "example"))
控制代码
首先设置swizconfig配置文件中的eventPackages=”example.event” 和strict=true。
[Mediate(event="FooEvent.FOO", properties="someValue")]
public function fooHandler(someValue:String){//...}
当swiz处理Mediate 元数据
.当example.event.FooEvent类存在
.当类有一个静态常量FOO
.当类有一个公共成员值
优先级
如果一个事件被多个类监听,你可能想控制动态监听调用的次序。
[Mediate(event="FooEvent.FOO", properties="someValue", priority="0")]
public function secondFooHandler(someValue:String):void{//...}
[Mediate(event="FooEvent.FOO", properties="someValue", priority="1")]
public function firstFooHandler(someValue:String):void
{
// I am called first because of higher priority
}
Passing the Event
Instead of passing properties of an event to the mediating function you can also pass the event itself.
[Mediate(event="FooEvent.FOO")]
public function fooEventHandler(event:FooEvent):void
控制事件流
事件对象还有更多的选择属性,比如停止广播,如下所示
[Mediate(event="FooEvent.FOO", properties="someValue", priority="0")]
public function secondFooHandler(someValue:String):void{//...}
[Mediate(event="FooEvent.FOO", priority="1")]
public function firstFooHandler(event:FooEvent):void
{
// when we call this the event won't bubble further meaning secondFooHandler will never be invoked
event.stopImmediatePropagation();
}
阅读(933) | 评论(0) | 转发(0) |