Return whether a dispatched command matched a listener

This commit is contained in:
Nathan Sobo
2014-09-18 12:40:55 -06:00
parent 67ff8f4382
commit 09b5ac887a
2 changed files with 12 additions and 0 deletions

View File

@@ -138,3 +138,10 @@ describe "CommandRegistry", ->
registry.dispatch(grandchild, 'command')
expect(called).toBe true
it "returns a boolean indicating whether any listeners matched the command", ->
registry.add '.grandchild', 'command', ->
expect(registry.dispatch(grandchild, 'command')).toBe true
expect(registry.dispatch(grandchild, 'bogus')).toBe false
expect(registry.dispatch(parent, 'command')).toBe false

View File

@@ -152,6 +152,7 @@ class CommandRegistry
handleCommandEvent: (event) =>
propagationStopped = false
immediatePropagationStopped = false
matched = false
currentTarget = event.target
syntheticEvent = Object.create event,
@@ -169,6 +170,8 @@ class CommandRegistry
.filter (listener) -> currentTarget.webkitMatchesSelector(listener.selector)
.sort (a, b) -> a.compare(b)
matched = true if matchingListeners.length > 0
for listener in matchingListeners
break if immediatePropagationStopped
listener.callback.call(currentTarget, syntheticEvent)
@@ -178,6 +181,8 @@ class CommandRegistry
break if propagationStopped
currentTarget = currentTarget.parentNode
matched
class CommandListener
constructor: (@selector, @callback) ->
@specificity = (SpecificityCache[@selector] ?= specificity(@selector))