From fbaf956e1fda3ae610ac01c50ebb39df0a2b3676 Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 5 Sep 2014 09:10:22 -0600 Subject: [PATCH] Handle .stopPropagation() being called on command events --- spec/command-registry-spec.coffee | 10 ++++++++++ src/command-registry.coffee | 6 +++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/spec/command-registry-spec.coffee b/spec/command-registry-spec.coffee index 28071932a..a76767523 100644 --- a/spec/command-registry-spec.coffee +++ b/spec/command-registry-spec.coffee @@ -57,3 +57,13 @@ describe "CommandRegistry", -> grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) expect(calls).toEqual ['.foo.bar', '.bar', '.foo'] + + it "stops bubbling through ancestors when .stopPropagation() is called on the event", -> + calls = [] + + registry.add 'command', '.parent', -> calls.push('parent') + registry.add 'command', '.child', -> calls.push('child-2') + registry.add 'command', '.child', (event) -> calls.push('child-1'); event.stopPropagation() + + grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) + expect(calls).toEqual ['child-1', 'child-2'] diff --git a/src/command-registry.coffee b/src/command-registry.coffee index 69776319d..cb29c4a2f 100644 --- a/src/command-registry.coffee +++ b/src/command-registry.coffee @@ -16,11 +16,14 @@ class CommandRegistry @listenersByCommandName[commandName].push(new CommandListener(selector, callback)) dispatchCommand: (event) => + propagationStopped = false + currentTarget = event.target + syntheticEvent = Object.create event, eventPhase: value: Event.BUBBLING_PHASE currentTarget: get: -> currentTarget + stopPropagation: value: -> propagationStopped = true - currentTarget = event.target loop matchingListeners = @listenersByCommandName[event.type] @@ -30,6 +33,7 @@ class CommandRegistry for listener in matchingListeners listener.callback.call(currentTarget, syntheticEvent) + break if propagationStopped break if currentTarget is @rootNode currentTarget = currentTarget.parentNode