実際の使用では、FilterResponseEvent:
など、特定のEventサブクラスのオブジェクトがリスナーに渡されます。<span>1</span> <span>use Symfony\Component\HttpKernel\Event\FilterResponseEvent; </span><span>2</span> <span>3</span> <span>public</span> function onKernelResponse(FilterResponseEvent $<span>event</span><span>) </span><span>4</span> <span>{ </span><span>5</span> $response = $<span>event</span>-><span>getResponse(); </span><span>6</span> $request = $<span>event</span>-><span>getRequest(); </span><span>7</span> <span>8</span> <span>//</span><span> ...</span> <span>9</span> }
<span>namespace</span><span> Acme\StoreBundle; final </span><span>class</span><span> StoreEvents { </span><span>/*</span><span>* * The store.order event is thrown each time an order is created * in the system. * * The event listener receives an * Acme\StoreBundle\Event\FilterOrderEvent instance. * * @var string </span><span>*/</span> <span>const</span> STORE_ORDER = <span>'</span><span>store.order</span><span>'</span><span>; }</span>
<span> 1</span> <span>namespace</span><span> Acme\StoreBundle\Event; </span><span> 2</span> <span> 3</span> <span>use Symfony\Component\EventDispatcher\Event; </span><span> 4</span> <span>use Acme\StoreBundle\Order; </span><span> 5</span> <span> 6</span> <span>class</span><span> FilterOrderEvent extends Event </span><span> 7</span> <span>{ </span><span> 8</span> <span>protected</span><span> $order; </span><span> 9</span> <span>10</span> <span>public</span><span> function __construct(Order $order) </span><span>11</span> <span> { </span><span>12</span> $<span>this</span>->order =<span> $order; </span><span>13</span> <span> } </span><span>14</span> <span>15</span> <span>public</span><span> function getOrder() </span><span>16</span> <span> { </span><span>17</span> <span>return</span> $<span>this</span>-><span>order; </span><span>18</span> <span> } </span><span>19</span> }
<span> 1</span> <span>use Acme\StoreBundle\StoreEvents; </span><span> 2</span> <span>use Acme\StoreBundle\Order; </span><span> 3</span> <span>use Acme\StoreBundle\Event\FilterOrderEvent; </span><span> 4</span> <span> 5</span> <span>//</span><span> the order is somehow created or retrieved</span> <span> 6</span> $order = <span>new</span><span> Order(); </span><span> 7</span> <span>//</span><span> ... </span><span> 8</span> <span> 9</span> <span>//</span><span> create the FilterOrderEvent and dispatch it</span> <span>10</span> $<span>event</span> = <span>new</span><span> FilterOrderEvent($order); </span><span>11</span> $dispatcher->dispatch(StoreEvents::STORE_ORDER, $<span>event</span>);
FilterOrderEvent对象作为参数传入到dispatch方法,现在,任何监听store.order事件的监听器都会接收到FilterOrderEvent对象,并通过调用getOrder方法获得order对象。
<span>1</span> <span>//</span><span> some listener class that's been registered for "store.order" event</span> <span>2</span> <span>use Acme\StoreBundle\Event\FilterOrderEvent; </span><span>3</span> <span>4</span> <span>public</span> function onStoreOrder(FilterOrderEvent $<span>event</span><span>) </span><span>5</span> <span>{ </span><span>6</span> $order = $<span>event</span>-><span>getOrder(); </span><span>7</span> <span>//</span><span> do something to or with the order</span> <span>8</span> }
<span>namespace</span><span> Acme\StoreBundle\Event; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; </span><span>class</span><span> StoreSubscriber implements EventSubscriberInterface { </span><span>public</span> <span>static</span><span> function getSubscribedEvents() { </span><span>return</span><span> array( </span><span>'</span><span>kernel.response</span><span>'</span> =><span> array( array(</span><span>'</span><span>onKernelResponsePre</span><span>'</span>, <span>10</span><span>), array(</span><span>'</span><span>onKernelResponseMid</span><span>'</span>, <span>5</span><span>), array(</span><span>'</span><span>onKernelResponsePost</span><span>'</span>, <span>0</span><span>), ), </span><span>'</span><span>store.order</span><span>'</span> => array(<span>'</span><span>onStoreOrder</span><span>'</span>, <span>0</span><span>), ); } </span><span>public</span> function onKernelResponsePre(FilterResponseEvent $<span>event</span><span>) { </span><span>//</span><span> ...</span> <span> } </span><span>public</span> function onKernelResponseMid(FilterResponseEvent $<span>event</span><span>) { </span><span>//</span><span> ...</span> <span> } </span><span>public</span> function onKernelResponsePost(FilterResponseEvent $<span>event</span><span>) { </span><span>//</span><span> ...</span> <span> } </span><span>public</span> function onStoreOrder(FilterOrderEvent $<span>event</span><span>) { </span><span>//</span><span> ...</span> <span> } }</span>
这个监听器类很简单,告诉了dispatcher监听了什么事件,还有监听的事件触发的方法。addSubscriber()方法把subscriber注册到dispatcher。
<span>1</span> <span>use Acme\StoreBundle\Event\StoreSubscriber; </span><span>2</span> <span>3</span> $subscriber = <span>new</span><span> StoreSubscriber(); </span><span>4</span> $dispatcher->addSubscriber($subscriber);
<code>onKernelResponsePre
, <code>onKernelResponseMid
, 和 <code><span>onKernelResponsePost三个方法就会先后执行。</span>
停止事件的传递
在一些情况下,监听器可以停止事件传递下去,防止后续的监听器被调用,换句话说,监听器必须通知dispatcher停止传递事件给后续的监听器。在监听器里面实现stopPropagation()方法:
<span>1</span> <span>use Acme\StoreBundle\Event\FilterOrderEvent; </span><span>2</span> <span>3</span> <span>public</span> function onStoreOrder(FilterOrderEvent $<span>event</span><span>) </span><span>4</span> <span>{ </span><span>5</span> <span>//</span><span> ...</span> <span>6</span> <span>7</span> $<span>event</span>-><span>stopPropagation(); </span><span>8</span> }
<span>1</span> $dispatcher->dispatch(<span>'</span><span>foo.event</span><span>'</span>, $<span>event</span><span>); </span><span>2</span> <span>if</span> ($<span>event</span>-><span>isPropagationStopped()) { </span><span>3</span> <span>//</span><span> ...</span> <span>4</span> }
As3 の EventDispatcher は、このクラスから継承されたオブジェクトのみがイベントを送信できる優れた機能です。たとえば、新しいオブジェクト A を作成し、このオブジェクトで特定の変更が発生した後に他のオブジェクトに通知したい場合は、dispatchEvent(new Event("yourEvent")) を使用して、他の場所で A を呼び出すことができます。 A のリスナー
A.addEventListener("yourEvent",yourfunction) このイベントはカスタマイズでき、一般的なオブジェクトは EventDispatcher のサブクラスです。
次の Web サイトには、公式の詳細な手順が記載されています。 r.html
方法は 2 つあります: 1. 別のコンポーネントで値を受け取るオブジェクトを ID 経由で直接呼び出し、それに値を割り当てます。例:
import flash.events.EventDispatcher;
/**
* コンポーネントのデータをロードするためのカスタム イベント クラス
* @author Yuan Jinlong
* @date 2010-07-08
*/
public class LoadDataEvent extends Event
{
public static constdispatcher:EventDispatcher=new EventDispatcher( );
public var data:Object = null;
public function LoadDataEvent(type:String, data:Object=null, bubbles:Boolean=false, cancelable :Boolean=false)
{
super(type, bubbles, cancelable);
this.EVENT_NAME = type;
this.data = data;
this.typeData = typeData;
this.tempData = tempData;
}
public をオーバーライドしますfunction clone():Event{
return new LoadDataEvent(type, bubbles, cancelable , data);
}