2

Polymer expressions allow you to write ternary operators. There is also a tokenList filter that can be used specifically on the class attribute.

It seems to me that the following two render the same thing.

ternary operator: <my-element class="{{ conditional ? 'some-style' : '' }}"></my-element>

tokenList filter: <my-element class="{{ {'some-style': conditional} | tokenList }}"></my-element>

I see authors of Topeka using ternary operators exclusively for id attributes, reserving tokenList filters for class attributes. I was just wondering if it's okay to use ternary operators in all circumstances; or if there's any special circumstances in which one is favored over the other.

1 Answers1

2

Suppose I have many boolean properties and I have to apply classes based on their value. If I use ternary operators, it would look like this

<my-element class="{{a? 'a': ''}} {{b?'b':''}}...{{z?'z': z}}"></my-element>

But with tokenList, it would just be a simple object being parsed by tokenList

<my-element class="{{ {a:a, b:b, c:c, ... z:z} | tokenList }}"><my-element>

You can see it's really useful for classes since you can have many classes within an element.

Side note though, tokenList and other filters or the filter syntax itself are not available anymore in Polymer 0.8 and up. You can use computed bindings instead.

Neil John Ramal
  • 3,734
  • 13
  • 20