1
votes

I am happy with new feature in Dart.

But, I would like to use in web component (Polymer)

Like this:

file.dart

enum Categoria {ID, DESCRICAO, NATUREZA, ID_SUBCATEGORIA_DA, DESCRICAO_SUBCATEGORIA_DA, ATIVO}

@published
Map itemList;

file.html

..

<template>
  <paper-input value="{{itemList[Categoria.ID]}}"></paper-input>
</template>

<script type="application/dart" src="file.dart">
</script>  

..

In the value on @published there is a Map itemList and I would like to use the enum Categoria.ID for key to find the value of the Map.

It´s possible in Web Component (polymer)?

1

1 Answers

2
votes

This isn't supported because types can't be used in Polymer bindings.

The following expressions aren't supported neither and and enums are basically syntactic sugar for similar code:

class SomeClass {
  static final someVal = 1;
}
<template if="{{x is SomeClass}}"></template>
<template if="{{x == SomeClass.someVal}}"></template>

As workaround you can add a method like

Categoria categoriaEnum(String val) =>
  Categoria.values.firstWhere((i) => 
      '${i}'.toString().toLowerCase().endsWith('.${val.toLowerCase()}'));

with an expression like

<paper-input value="{{itemList[categoriaEnum('ID')]}}"></paper-input>

You could also move the method to a mixin and make each of your Polymer elements support enums this way by just applying the mixing. Still not very beautiful though.