Permalink
Browse files

Fix layer.off bug: https://twitter.com/fictionism/status/466996305743…

  • Loading branch information...
koenbok committed May 15, 2014
1 parent 3d42703 commit 7f31ec816ef2e2093ac6269492b4d8a01fb40175
Showing with 79 additions and 3 deletions.
  1. +1 −0 framer/Framer.coffee
  2. +15 −3 framer/Layer.coffee
  3. +1 −0 test/init.coffee
  4. +41 −0 test/tests/EventEmitterTest.coffee
  5. +21 −0 test/tests/LayerTest.coffee
@@ -15,6 +15,7 @@ _.extend window, Framer if window
# Framer level modules
Framer.Config = (require "./Config").Config
Framer.EventEmitter = (require "./EventEmitter").EventEmitter
Framer.BaseClass = (require "./BaseClass").BaseClass
Framer.LayerStyle = (require "./LayerStyle").LayerStyle
Framer.AnimationLoop = (require "./AnimationLoop").AnimationLoop
@@ -5,6 +5,7 @@ Utils = require "./Utils"
{Config} = require "./Config"
{Defaults} = require "./Defaults"
{BaseClass} = require "./BaseClass"
{EventEmitter} = require "./EventEmitter"
{Animation} = require "./Animation"
{Frame} = require "./Frame"
{LayerStyle} = require "./LayerStyle"
@@ -560,11 +561,15 @@ class exports.Layer extends BaseClass
addListener: (event, originalListener) =>
# Modify the scope to be the calling object, just like jquery
# also add the object as the last argument
# # Modify the scope to be the calling object, just like jquery
# # also add the object as the last argument
listener = (args...) =>
originalListener.call @, args..., @
# Because we modify the listener we need to keep track of it
# so we can find it back when we want to unlisten again
originalListener.modifiedListener = listener
# Listen to dom events on the element
super event, listener
@_element.addEventListener event, listener
@@ -577,7 +582,14 @@ class exports.Layer extends BaseClass
@ignoreEvents = false
removeListener: (event, listener) ->
super
# If the original listener was modified, remove that
# one instead
if listener.modifiedListener
listener = listener.modifiedListener
super event, listener
@_element.removeEventListener event, listener
@_eventListeners[event] = _.without @_eventListeners[event], listener
@@ -1,5 +1,6 @@
window.console.debug = (v) ->
require "./tests/EventEmitterTest"
require "./tests/UtilsTest"
require "./tests/BaseClassTest"
require "./tests/FrameTest"
@@ -0,0 +1,41 @@
describe "EventEmitter", ->
it "should listen", ->
tester = new Framer.EventEmitter
count = 0
handler = -> count++
tester.on "test", handler
tester.emit "test"
count.should.equal 1
it "should stop listening", ->
tester = new Framer.EventEmitter
count = 0
handler = -> count++
tester.on "test", handler
tester.emit "test"
count.should.equal 1
tester.off "test", handler
tester.emit "test"
count.should.equal 1
it "should listen once", ->
tester = new Framer.EventEmitter
count = 0
handler = -> count++
tester.once "test", handler
tester.emit "test"
tester.emit "test"
tester.emit "test"
count.should.equal 1
@@ -288,6 +288,27 @@ describe "Layer", ->
simulate.click myLayer._element
it "should remove events", ->
layer = new Layer
clickCount = 0
handler = ->
clickCount++
layer.on "test", handler
layer.emit "test"
clickCount.should.equal 1
layer.off "test", handler
layer.emit "test"
clickCount.should.equal 1
describe "Hierarchy", ->
it "should insert in dom", ->

0 comments on commit 7f31ec8

Please sign in to comment.