Allow multiple commands to be registered by passing an object

This commit is contained in:
Nathan Sobo
2014-09-05 09:54:02 -06:00
parent aee33fc126
commit 43d3082d4e
2 changed files with 26 additions and 1 deletions

View File

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

View File

@@ -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] = []