diff --git a/spec/command-registry-spec.coffee b/spec/command-registry-spec.coffee index 705f89966..b39678c91 100644 --- a/spec/command-registry-spec.coffee +++ b/spec/command-registry-spec.coffee @@ -92,3 +92,21 @@ describe "CommandRegistry", -> disposable2.dispose() grandchild.dispatchEvent(new CustomEvent('command', bubbles: true)) expect(calls).toEqual [] + + it "allows multiple commands to be registered under one selector when called with an object", -> + calls = [] + + disposable = registry.add '.child', + 'command-1': -> calls.push('command-1') + 'command-2': -> calls.push('command-2') + + grandchild.dispatchEvent(new CustomEvent('command-1', bubbles: true)) + grandchild.dispatchEvent(new CustomEvent('command-2', bubbles: true)) + + expect(calls).toEqual ['command-1', 'command-2'] + + calls = [] + disposable.dispose() + grandchild.dispatchEvent(new CustomEvent('command-1', bubbles: true)) + grandchild.dispatchEvent(new CustomEvent('command-2', bubbles: true)) + expect(calls).toEqual [] diff --git a/src/command-registry.coffee b/src/command-registry.coffee index 621461429..2a607fb52 100644 --- a/src/command-registry.coffee +++ b/src/command-registry.coffee @@ -1,4 +1,4 @@ -{Disposable} = require 'event-kit' +{Disposable, CompositeDisposable} = require 'event-kit' {specificity} = require 'clear-cut' SequenceCount = 0 @@ -10,6 +10,13 @@ class CommandRegistry @listenersByCommandName = {} add: (selector, commandName, callback) -> + if typeof commandName is 'object' + commands = commandName + disposable = new CompositeDisposable + for commandName, callback of commands + disposable.add @add(selector, commandName, callback) + return disposable + unless @listenersByCommandName[commandName]? @rootNode.addEventListener(commandName, @dispatchCommand, true) @listenersByCommandName[commandName] = []