Only trigger bindings on the closest ancestor node of an event target.

Say we have a structure like:

div.parent
  div.child
    div.grandchild

And we have two mappings:

.parent { x: foo }
.child { x:bar }

If there's an event originating on grandchild, it will *only* trigger
bar, because that selector selects a closer ancestor.
This commit is contained in:
Nathan Sobo
2012-01-09 17:49:00 -08:00
committed by Corey Johnson & Nathan Sobo
parent 9c98e971fc
commit f5be55e000
3 changed files with 27 additions and 17 deletions

View File

@@ -15,9 +15,8 @@ class BindingSet
constructor: (@selector, @bindings) ->
commandForEvent: (event) ->
if @selectorMatchesEvent(event)
for pattern, command of @bindings
return command if @eventMatchesPattern(event, pattern)
for pattern, command of @bindings
return command if @eventMatchesPattern(event, pattern)
null
eventMatchesPattern: (event, pattern) ->
@@ -44,10 +43,3 @@ class BindingSet
which: charCode
key: key
selectorMatchesEvent: (event) ->
currentNode = event.target
while currentNode
return true if $(currentNode).is(@selector)
currentNode = currentNode.parentNode
false

View File

@@ -12,8 +12,12 @@ class KeyEventHandler
@bindingSets.push(new BindingSet(selector, bindings))
handleKeypress: (event) ->
for bindingSet in @bindingSets
if command = bindingSet.commandForEvent(event)
$(event.target).trigger(command)
return
currentNode = event.target
while currentNode
for bindingSet in @bindingSets
if $(currentNode).is(bindingSet.selector)
if command = bindingSet.commandForEvent(event)
$(event.target).trigger(command)
return
currentNode = currentNode.parentNode