Hi guys, is there a way to check if a layer has states defined?
Each layer has at least one state: the "default" state it gets when it’s created. To access all states try layer.states._states.
I'm really trying to check if a layer has more than one state, the default state…
Gotcha. Try this: numberOfStates = 0
for state of layer.states._states numberOfStates++
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…
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"
Oh, thanks a lot Andy. That is exactly what I needed :)
Good luck :)
I have a related question: Is there a way to check if a layer has events?
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?
No, I meant:
if layer.hasEvents() do somethingorif layer.Events > 0 do something
Events like click, drag, or whatever. Just need to check if a layer has events defined.
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
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
You could check for listeners with a jquery data("events") function, but I’m not sure if there’s an easier of doing that.