I seem to recall recently being surprised to learn that you can capture an
event that doesn't bubble.

I just tried in Flash, and I can confirm that a non-bubbling event can be
seen by a parent during the capture phase.

var sprite:Sprite = new Sprite();
var child:Sprite = new Sprite();
sprite.addChild(child);

sprite.addEventListener(Event.CHANGE, function(event:Event):void
{
    trace("parent (capture):", event.eventPhase);
}, true);

child.dispatchEvent(new Event(Event.CHANGE, false));



--
Josh Tynjala
Bowler Hat LLC <https://bowlerhat.dev>


On Wed, Dec 25, 2019 at 2:25 AM Harbs <harbs.li...@gmail.com> wrote:

> Capture phase will only work if the event.bubbles is true.
>
> I think that’s correct behavior. Someone please correct me if I’m wrong.
>
> > On Dec 25, 2019, at 12:23 PM, ha...@apache.org wrote:
> >
> > This is an automated email from the ASF dual-hosted git repository.
> >
> > harbs pushed a commit to branch develop
> > in repository https://gitbox.apache.org/repos/asf/royale-asjs.git
> >
> >
> > The following commit(s) were added to refs/heads/develop by this push:
> >     new 2f8ef75  Fixed event bubbling
> > 2f8ef75 is described below
> >
> > commit 2f8ef759d700f439fa40cc2abf6aefbb04ab4bdb
> > Author: Harbs <ha...@in-tools.com>
> > AuthorDate: Wed Dec 25 12:23:13 2019 +0200
> >
> >    Fixed event bubbling
> > ---
> > .../org/apache/royale/events/EventDispatcher.as    | 72
> ++++++----------------
> > 1 file changed, 18 insertions(+), 54 deletions(-)
> >
> > diff --git
> a/frameworks/projects/Core/src/main/royale/org/apache/royale/events/EventDispatcher.as
> b/frameworks/projects/Core/src/main/royale/org/apache/royale/events/EventDispatcher.as
> > index 34871d5..510b531 100644
> > ---
> a/frameworks/projects/Core/src/main/royale/org/apache/royale/events/EventDispatcher.as
> > +++
> b/frameworks/projects/Core/src/main/royale/org/apache/royale/events/EventDispatcher.as
> > @@ -90,8 +90,23 @@ package org.apache.royale.events
> >                                       //console.log("assigned target to
> event ",event);
> >                               }
> >                       } else return false;
> > +                     var ancestorsTree:Array = null;
> > +                     if(event1.bubbles)
> > +                     {
> > +                             var ancestor:Object =
> this.getParentEventTarget();
> > +                             if (ancestor) {
> > +                     ancestorsTree = [];
> > +                     var ancestorCount:int = 1;
> > +                         for (; ancestor; ancestor =
> ancestor.getParentEventTarget()) {
> > +                             ancestorsTree.push(ancestor);
> > +                     //      goog.asserts.assert(
> > +          //                 (++ancestorCount <
> goog.events.EventTarget.MAX_ANCESTORS_),
> > +          // 'infinite loop');
> > +                     }
> > +                     }
> > +                     }
> >
> > -                     return super.dispatchEvent(event1);
> > +                     return
> goog.events.EventTarget.dispatchEventInternal_(_dispatcher, event1,
> ancestorsTree);
> >               }
> >               /**
> >                * @royaleignorecoercion org.apache.royale.core.IChild
> > @@ -129,59 +144,8 @@ package org.apache.royale.events
> >               }
> >
> >               public function toString():String
> > -        {
> > -            return "[object Object]";
> > -        }
> > -             private static function installOverride():Boolean{
> > -                     goog.events.EventTarget.dispatchEventInternal_ =
> dispatchEventInternal;
> > -                     return true;
> > +             {
> > +                             return "[object Object]";
> >               }
> > -             private static var overrideInstalled:Boolean =
> installOverride();
> > -             /**
> > - * Dispatches the given event on the ancestorsTree.
> > - *
> > - * @param {!Object} target The target to dispatch on.
> > - * @param {goog.events.Event|Object|string} e The event object.
> > - * @param {Array<goog.events.Listenable>=} opt_ancestorsTree The
> ancestors
> > - *     tree of the target, in reverse order from the closest ancestor
> > - *     to the root event target. May be null if the target has no
> ancestor.
> > - * @return {boolean} If anyone called preventDefault on the event
> object (or
> > - *     if any of the listeners returns false) this will also return
> false.
> > - * @private
> > - */
> > -private static function dispatchEventInternal(target:EventDispatcher,
> e:org.apache.royale.events.Event, opt_ancestorsTree:Array):Boolean {
> > -  /** @suppress {missingProperties} */
> > -  var type:String = e.type;
> > -
> > -  var rv:Boolean = true, currentTarget:Object;
> > -
> > -  // Executes all capture listeners on the ancestors, if any.
> > -  if (opt_ancestorsTree) {
> > -    for (var i:int = opt_ancestorsTree.length - 1;
> !e.propagationStopped_ && i >= 0;
> > -         i--) {
> > -      currentTarget = e.currentTarget = opt_ancestorsTree[i];
> > -      rv = currentTarget.fireListeners(type, true, e) && rv;
> > -    }
> > -  }
> > -
> > -  // Executes capture and bubble listeners on the target.
> > -  if (!e.propagationStopped_) {
> > -    currentTarget = e.currentTarget = target;
> > -    rv = currentTarget.fireListeners(type, true, e) && rv;
> > -    if (!e.propagationStopped_) {
> > -      rv = currentTarget.fireListeners(type, false, e) && rv;
> > -    }
> > -  }
> > -
> > -  // Executes all bubble listeners on the ancestors, if any.
> > -  if (opt_ancestorsTree && e.bubbles) {
> > -    for (i = 0; !e.propagationStopped_ && i < opt_ancestorsTree.length;
> i++) {
> > -      currentTarget = e.currentTarget = opt_ancestorsTree[i];
> > -      rv = currentTarget.fireListeners(type, false, e) && rv;
> > -    }
> > -  }
> > -
> > -  return rv;
> > -};
> >       }
> > }
> >
>
>

Reply via email to