So how do you pass list data into a Polymer element?
I was pretty darn certain that starting with a smaller
<x-pizza-toppings>
element in the model driven view chapter of Patterns in Polymer would be brilliant. And then I tried using it.Adding it the page works just fine:
But this was originally meant to be a composable, internal-only element for use in a whole pizza builder element,
<x-pizza>
. To ensure that all topping elements (first half, second half, whole pizza) used the same list of ingredients, I created the list in <x-pizza>
:@CustomTag('x-pizza') class XPizza extends PolymerElement { final List ingredients = [ 'pepperoni', 'sausage', 'green peppers' ]; // ... }And then bound that variable to all instances of
<x-pizza-toppings>
in the corresponding template:<link rel="import" href="x-pizza-toppings.html"> <polymer-element name="x-pizza"> <template> <!-- ... --> <x-pizza-toppings id="firstHalfToppings" name="First Half Toppings" ingredients="{{ingredients}}"></x-pizza-toppings> <!-- ... --> </template> <script type="application/dart;component=1" src="x_pizza.dart"></script> </polymer-element>But how do you do that from the outside when you don't have a List, Array or Object? How can I bind a list of strings (the ingredients) to the
ingredients
attribute of <x-pizza-toppings>
?I may have found the answer by accident three nights ago. While fiddling with Polymer.dart JavaScript interoperablity, I found that the only way to make certain things work was to bind attributes as JSON instead of native programming types. Perhaps that will work here?
<x-pizza-toppings
ingredients="['pepperoni', 'sausage', 'green peppers']">
</x-pizza-toppings>
And indeed that does work:Cool! But does it work in the JavaScript version of Polymer as well? Yup:
It makes sense that Polymer would behave this way, but it is good to have confirmation. I cannot find any documentation for this behavior, but binding JSON seems a reasonable thing to do. And, since it behaves like this in both Dart and JavaScript, it seems safe to use this feature.
So, phew! I can finish off the MDV chapter rewrite without any further worries. Well, until I try the next easy thing...
Day #32
No comments:
Post a Comment