diff --git a/spec/command-registry-spec.coffee b/spec/command-registry-spec.coffee index 1bb065359..98c480375 100644 --- a/spec/command-registry-spec.coffee +++ b/spec/command-registry-spec.coffee @@ -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 diff --git a/src/command-registry.coffee b/src/command-registry.coffee index dc173dc97..11dec2dca 100644 --- a/src/command-registry.coffee +++ b/src/command-registry.coffee @@ -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))