Does anyone else have issues with unbinding events in Framer? I've recently run into this issue quite a few times. Is this a known issue?
Hey Peter! When you say unbind, do you mean layer.off?
How are setting the original listener and what issues are you running into?
card.on Events.DragStart, dragStart.bind(null, this)card.off Events.DragStart, dragStart
seems fairly straightforward, but perhaps I'm doing something obviously wrong
Peter Lada No worries! Let me explain it in the best way I can (I'm learning too).
If you wish to keep the context of this, you can use Coffeescript's fat arrow – http://michaeljosephkramer.com/2013/03/12/the-simplified-fat-arrow-guide-for-coffeescript/. So you would use:
card.on Events.DragStart, =>
Based on my experience, binding outside of a creating a class isn't really an issue. From what I've seen, in Framer events, the last argument is always the reference to the layer. This is also the layer too. So you can do something like:
card.on Events.DragStart, (event, layer) -># layer is referencing cardprint layer # this is also cardprint this
As for unbinding, to my knowledge, you can't remove/unbind a listener that is anonymous function. That's simply because it has no reference to the function in order to actually remove it. Binding always creates an anonymous function so you're stuck there.
The only way to be able to remove an event listener is to give it an explicit handler function.
printFunc = ->print "Printing!"
# On DragStart, the event is handled inside of printFunccard.on Events.DragStart, printFunc
# Later in my code...# Since an explicit handler is given, it has a reference and removes it.card.off Events.DragStart, printFunc
Let me know if this addresses your question!
Joshua right on the spot, as always :)
You can also use .once instead of .on, which basically fires the event just one time:
card.once Events.Click, foo
I always forget about .once... Thanks for the reminder guys.
It wasnt a fat arrow issue, it was that i was rebinding the event through another event listener which I forgot about :) thanks though! Also, I didnt realize .once was also a framer thing, thats cool.
Right on man! Glad you got it figured out.