Handle .stopPropagation() being called on command events

This commit is contained in:
Nathan Sobo 2014-09-05 09:10:22 -06:00
parent 5eb22520f1
commit fbaf956e1f
2 changed files with 15 additions and 1 deletions

View File

@ -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']

View File

@ -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