This is a read-only archive of the Framer Community on Facebook.

What is Framer? Join the Community
Return to index
Jon Madison
Posted Oct 15 - Read on Facebook

Need another of eyes on this. trying to dynamically register event handlers for two different events on one layer. Doing this separately works fine, but as soon as i create a loop to handle the events things breaks down. I'm guessing it's an issue with my CoffeeScript knowledge (of scope?) at any rate, here's the code, v simple:

layerA = new Layer
x: Align.center
y: Align.center

e = "Touch"

eventOptions = [
{
"eventName": "#{e}Start",
"handler": () ->
print "began event"
},
{
"eventName": "#{e}End",
"handler": () ->
print "ended event"
}
]

for eo in eventOptions
print "attaching handler on event", eo
layerA.on Events[eo.eventName], ->
print "calling", eo.handler, " due to ", eo.eventName
eo.handler()

Somehow the "TouchEnd" event is being fired on both touch start and touch end. Any ideas?

http://share.framerjs.com/czyp79b2lqpt/

is the correct share link -- fb is buggin

3 Comments

Jon Madison

I did find a way that works for dynamically registering multiple events, by calling the simplified version of event registration:

layerA.on(Events[eo.eventName], eo.handler)

Radek Kyselý

When you create something with "for" loop, Events listener's actions react only to the last of that "something" created.
Here you create options of touch events, so you need to lock each one with "do", and in each loop your event is stored in variable "eo" (which you're trying to fire inside the event) :)

Jordan Robert Dobson

You shouldn't need do -> here if you used @

Read the entire post on Facebook