この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

コンポーネントイベントバブル

コンポーネントイベントバブルは、ブラウザでの標準のイベントバブルと似ています。コンポーネントイベントが起動されると、そのイベントを起動したコンポーネントがイベントを処理できます。その後イベントはバブルアップし、コンテインメント階層内のバブルイベントを受信するコンポーネントによって処理されます。

イベントバブルルール

コンポーネントイベントは、コンテインメント階層内のすべての親が処理できるわけではありません。代わりに、コンテインメント階層内のすべてのファセット値プロバイダにバブルします。ファセット値プロバイダとは、イベントの起動コンポーネントを参照するマークアップが含まれる、最も外側のコンポーネントのことです。わかりやすく、例を使用して説明します。

docsample:eventBubblingParent には docsample:eventBubblingChild が含まれ、docsample:eventBubblingChild には docsample:eventBubblingGrandchild が含まれます。

1<!--docsample:eventBubblingParent-->
2<aura:component>
3    <docsample:eventBubblingChild>
4        <docsample:eventBubblingGrandchild />
5    </docsample:eventBubblingChild>
6</aura:component>

docsample:eventBubblingGrandchild がコンポーネントイベントを起動すると、このコンポーネント自体がイベントを処理します。次に、イベントはコンテインメント階層をバブルアップします。docsample:eventBubblingChild には docsample:eventBubblingGrandchild が含まれますが、マークアップの最も外側のコンポーネントではないことが原因でファセット値プロバイダにはならないため、バブルイベントを処理できません。docsample:eventBubblingParent は、 docsample:eventBubblingChild がそのマークアップ内にあるため、ファセット値プロバイダです。docsample:eventBubblingParent はイベントを処理できます。

バブルイベントを処理する

コンポーネントイベントを起動したコンポーネントは、<aura:registerEvent> タグを使用してイベントを起動したことを登録します。
1<aura:component>
2    <aura:registerEvent name="bubblingEvent" type="docsample:compEvent" />
3</aura:component>

バブルコンポーネントイベントを処理するコンポーネントは、<aura:handler> タグを使用してそのクライアント側コントローラの処理アクションを割り当てます。

1<aura:component>
2    <aura:handler name="bubblingEvent" event="docsample:compEvent" action="{!c.handleBubbling}"/>
3</aura:component>

<aura:handler>name 属性は、イベントを起動するコンポーネントの <aura:registerEvent> タグの name 属性に一致する必要があります。

メモ

イベントバブルの例

自分でいろいろと試せるように、この例のコード全体に目を通しましょう。

このサンプルコードでは、docsample 名前空間を使用しています。自分の組織に名前空間がある場合は、その名前空間を使用してください。ない場合は、デフォルトの名前空間 c を使用します。

メモ

最初に、単純なコンポーネントイベントを定義します。

1<!--docsample:compEvent-->
2<aura:event type="COMPONENT">
3    <!--simple event with no attributes-->
4</aura:event>

docsample:eventBubblingEmitter は、docsample:compEvent を起動するコンポーネントです。

1<!--docsample:eventBubblingEmitter-->
2<aura:component>
3    <aura:registerEvent name="bubblingEvent" type="docsample:compEvent" />
4    <ui:button press="{!c.fireEvent}" label="Start Bubbling"/>
5</aura:component>

docsample:eventBubblingEmitter のコントローラは次のようになります。ボタンをクリックすると、マークアップに登録された bubblingEvent イベントが起動します。

1/*eventBubblingEmitterController.js*/
2{
3    fireEvent : function(cmp) {
4        var cmpEvent = cmp.getEvent("bubblingEvent");
5        cmpEvent.fire();
6    }
7}

docsample:eventBubblingGrandchild には docsample:eventBubblingEmitter が含まれ、<aura:handler> を使用してイベントのハンドラを割り当てます。

1<!--docsample:eventBubblingGrandchild-->
2<aura:component>
3    <aura:handler name="bubblingEvent" event="docsample:compEvent" action="{!c.handleBubbling}"/>
4
5    <div class="grandchild">
6        <docsample:eventBubblingEmitter />
7    </div>
8</aura:component>

docsample:eventBubblingGrandchild のコントローラは次のようになります。

1/*eventBubblingGrandchildController.js*/
2{
3    handleBubbling : function(component, event) {
4        console.log("Grandchild handler for " + event.getName());
5    }
6}

ハンドラがコールされると、コントローラはイベント名をログに記録します。

docsample:eventBubblingChild のマークアップは次のようになります。docsample:eventBubblingGrandchild は、この例で後から docsample:eventBubblingParent を作成するときに docsample:eventBubblingChild のボディとして渡します。

1<!--docsample:eventBubblingChild-->
2<aura:component>
3    <aura:handler name="bubblingEvent" event="docsample:compEvent" action="{!c.handleBubbling}"/>
4
5    <div class="child">
6        {!v.body}
7    </div>
8</aura:component>

docsample:eventBubblingChild のコントローラは次のようになります。

1/*eventBubblingChildController.js*/
2{
3    handleBubbling : function(component, event) {
4        console.log("Child handler for " + event.getName());
5    }
6}

docsample:eventBubblingParent には docsample:eventBubblingChild が含まれ、docsample:eventBubblingChild には docsample:eventBubblingGrandchild が含まれます。

1<!--docsample:eventBubblingParent-->
2<aura:component>
3    <aura:handler name="bubblingEvent" event="docsample:compEvent" action="{!c.handleBubbling}"/>
4    
5    <div class="parent">
6        <docsample:eventBubblingChild>
7            <docsample:eventBubblingGrandchild />
8        </docsample:eventBubblingChild>
9    </div>
10</aura:component>

docsample:eventBubblingParent のコントローラは次のようになります。

1/*eventBubblingParentController.js*/
2{
3    handleBubbling : function(component, event) {
4        console.log("Parent handler for " + event.getName());
5    }
6}

次に、コードを実行したらどうなるか確認しましょう。

  1. ブラウザで、docsample:eventBubblingParent に移動します。<docsample:eventBubblingParent /> が含まれる .app リソースを作成します。
  2. docsample:eventBubblingEmitter のマークアップに含まれる [バブル設定を開始] ボタンをクリックします。
  3. ブラウザのコンソールで出力を確認します。
    1Grandchild handler for bubblingEvent
    2Parent handler for bubblingEvent

docsample:compEvent イベントは、コンテインメント階層のファセット値プロバイダである docsample:eventBubblingGrandchild および docsample:eventBubblingParent にバブルされます。イベントが docsample:eventBubblingChild によって処理されることはありません。docsample:eventBubblingChilddocsample:eventBubblingParent のマークアップに含まれますが、そのマークアップの最も外側にあるコンポーネントではないためにファセット値プロバイダにならないからです。

イベント伝達を停止する

他のコンポーネントへのイベントバブルを停止するには、Event オブジェクトの stopPropagation() メソッドを使用します。

たとえば、伝達を停止するには docsample:eventBubblingGrandchild のコントローラを編集します。

1/*eventBubblingGrandchildController.js*/
2{
3    handleBubbling : function(component, event) {
4        console.log("Grandchild handler for " + event.getName());
5        event.stopPropagation();
6    }
7}

次に、docsample:eventBubblingParent に移動して、[バブル設定を開始] ボタンをクリックします。

ブラウザのコンソールで出力を確認します。

1Grandchild handler for bubblingEvent

イベントは docsample:eventBubblingParent コンポーネントにバブルアップされなくなりました。