Permalink
Browse files

Merge pull request #130 from elliottkember/multipleEvents

myLayer.on "change:x", "change:y", ->
  • Loading branch information...
koenbok committed Oct 20, 2014
2 parents 5b6aed1 + 7bf6967 commit 1b13e7ff2029bb2ab559b51fadfbef491170190a
Showing with 42 additions and 18 deletions.
  1. +26 −18 framer/Layer.coffee
  2. +16 −0 test/tests/LayerTest.coffee
@@ -699,7 +699,7 @@ class exports.Layer extends BaseClass
##############################################################
## EVENTS
addListener: (eventName, originalListener) =>
addListener: (eventNames..., originalListener) =>
# To avoid an error in Framer Studio we return if no originalListener was given
if not originalListener
@@ -714,32 +714,40 @@ class exports.Layer extends BaseClass
# so we can find it back when we want to unlisten again
originalListener.modifiedListener = listener
# Listen to dom events on the element
super eventName, listener
@_context.eventManager.wrap(@_element).addEventListener(eventName, listener)
eventNames = [eventNames] if typeof eventNames == 'string'
@_eventListeners ?= {}
@_eventListeners[eventName] ?= []
@_eventListeners[eventName].push listener
# # Listen to dom events on the element
for eventName in eventNames
do (eventName) =>
super eventName, listener
@_context.eventManager.wrap(@_element).addEventListener(eventName, listener)
# We want to make sure we listen to these events, but we can safely
# ignore it for change events
if not _.startsWith eventName, "change:"
@ignoreEvents = false
@_eventListeners ?= {}
@_eventListeners[eventName] ?= []
@_eventListeners[eventName].push listener
removeListener: (eventName, listener) ->
# We want to make sure we listen to these events, but we can safely
# ignore it for change events
if not _.startsWith eventName, "change:"
@ignoreEvents = false
removeListener: (eventNames..., listener) ->
# If the original listener was modified, remove that
# one instead
if listener.modifiedListener
listener = listener.modifiedListener
super eventName, listener
@_context.eventManager.wrap(@_element).removeEventListener(eventName, listener)
if @_eventListeners
@_eventListeners[eventName] = _.without @_eventListeners[eventName], listener
eventNames = [eventNames] if typeof eventNames == 'string'
for eventName in eventNames
do (eventName) =>
super eventName, listener
@_context.eventManager.wrap(@_element).removeEventListener(eventName, listener)
if @_eventListeners
@_eventListeners[eventName] = _.without @_eventListeners[eventName], listener
removeAllListeners: ->
@@ -341,6 +341,22 @@ describe "Layer", ->
layer.ignoreEvents.should.equal true
layer.style["pointerEvents"].should.equal "none"
it "should listen to multiple events", ->
layer = new Layer()
count = 0
handler = -> count++
layer.on "click", "tap", handler
console.log layer._eventListeners
layer.emit "click"
layer.emit "tap"
count.should.equal 2
it "should not listen to events until a listener is added", ->
layer = new Layer()

0 comments on commit 1b13e7f

Please sign in to comment.