フレックスのターゲットと現在のターゲットの違いを教えてもらえますか?
3 に答える
確かに、私もこれに問題がありました。プロパティは、currentTarget
イベントハンドラーを登録したIEventListenerです。これtarget
は、現在処理しているイベントをディスパッチしたものです。したがって、currentTarget
変更はありtarget
ません。
次の例を確認してください。
サンプルアプリ
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="addListeners()">
<mx:Script>
<![CDATA[
protected function addListeners():void
{
greatGrandParent.addEventListener(Event.COMPLETE, completeHandler);
grandParent.addEventListener(Event.COMPLETE, completeHandler);
aParent.addEventListener(Event.COMPLETE, completeHandler);
child.addEventListener(Event.COMPLETE, completeHandler);
// dispatch event that "bubbles", second param is "true"
// dispatched from child
child.dispatchEvent(new Event(Event.COMPLETE, true));
}
protected function completeHandler(event:Event):void
{
trace("target: ", event.target + ", currentTarget: ", event.currentTarget);
}
]]>
</mx:Script>
<mx:Panel id="greatGrandParent">
<mx:Panel id="grandParent">
<mx:Panel id="aParent">
<mx:Button id="child"/>
</mx:Panel>
</mx:Panel>
</mx:Panel>
</mx:Application>
出力
target: MyApp.greatGrandParent.grandParent.aParent.child, currentTarget: MyApp.greatGrandParent.grandParent.aParent.child
target: MyApp.greatGrandParent.grandParent.aParent.child, currentTarget: MyApp.greatGrandParent.grandParent.aParent
target: MyApp.greatGrandParent.grandParent.aParent.child, currentTarget: MyApp.greatGrandParent.grandParent
target: MyApp.greatGrandParent.grandParent.aParent.child, currentTarget: MyApp.greatGrandParent
これは表示オブジェクトの単純なツリーであり、アプリの準備ができたら次のようにします。
- ツリーの各コンポーネントに同じイベントのリスナーを追加します。
- 任意のイベントをディスパッチします(デモンストレーション用)。私はを選び
Event.COMPLETE
ました。
すべてが同じイベントのeventHandlerを登録し、bubbles
true(new Event(type, bubbles)
)に設定したので、子からgreatGrandParent以降まで、のイベントハンドラーを登録したツリー内のすべてが、Event.COMPLETE
そのメソッドを実行しますcompleteHandler
。イベントはチェーンを上に移動し、次に下に移動します。これtarget
はイベントをディスパッチしたものであるため、イベントをディスパッチしたため、child
一定である必要があります。currentTarget
何が変わるかです。
つまり、FlexでDataGridをロールオーバーするときにチェックしたい場合、DataGridのitemRendererの1つにあるチェックボックスをいつロールオーバーするかを知りたいということです。1つの方法は、のすべてのitemRendererのチェックボックスにEventListenerを追加することですMouseEvent.ROLL_OVER
。もう1つの方法は、のDataGrid自体にEventListenerを追加し、イベントのターゲットMouseEvent.ROLL_OVER
が何であるかを確認することです。
protected function dataGrid_rollOverHandler(event:MouseEvent):void
{
// event.currentTarget is DataGrid
if (event.target is CheckBox)
trace("rolled over checkbox!");
}
それが私がよく使う方法event.target
です。
お役に立てば幸い、ランス
このような質問をする前に、次のサイトのチュートリアル(http://www.adobe.com/devnet/flex/videotraining/ )でFlexの概要を確認する必要があります。あなたの質問は1日目にカバーされます。