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

What is Framer? Join the Community
Return to index
Fran Pérez
Posted Jan 29 - Read on Facebook

Hi guys, is there a way to check if a layer has states defined?

13 Comments

Andy Jakubowski

Each layer has at least one state: the "default" state it gets when it’s created. To access all states try layer.states._states.

Fran Pérez

I'm really trying to check if a layer has more than one state, the default state…

Andy Jakubowski

Gotcha. Try this:
numberOfStates = 0

for state of layer.states._states
numberOfStates++

print numberOfStates

Fran Pérez

No, what I'm trying to do is more like: if layer.states._states.length > 1 then do something

But I can't get the .length to work…

Andy Jakubowski

Right. layer.states._states is an object full of state keys, so you can count them like so:

if Object.keys(layer.states._states).length > 1
print "More than one state"

Fran Pérez

Oh, thanks a lot Andy. That is exactly what I needed :)

Andy Jakubowski

Good luck :)

Fran Pérez

I have a related question: Is there a way to check if a layer has events?

Andy Jakubowski

Each layer has related events you can listen to by default. These are the things that can happen to a layer and that could can listen for. Is that what you mean?

Fran Pérez

No, I meant:

if layer.hasEvents() do something
or
if layer.Events > 0 do something

Events like click, drag, or whatever. Just need to check if a layer has events defined.

Andy Jakubowski

Events always exist. You don’t define events for a layer, but you can „listen to” them. For example, you can do something if Framer detects a click:

layer.on Events.Click, () ->
print "Layer was clicked"

If you want to specifically find out if an layer has event listeners defined, that’s a little more complicated... But maybe there’s a different way of solving the problem without asking that question?

More info about events here: http://framerjs.com/docs/#events.events

Fran Pérez

Mmmm let me rephrase my question. I would like to achieve this, but for any type of event.

if typeof layer.onclick is "function"
layer has an event handler already set for the onclick event

Andy Jakubowski

You could check for listeners with a jquery data("events") function, but I’m not sure if there’s an easier of doing that.

More: http://stackoverflow.com/questions/11430672/javascript-how-to-check-if-event-already-added

Read the entire post on Facebook