googlearchive / TemplateBinding

TemplateBinding Prolyfill
290 stars 56 forks source link

Problem aliasing concrete values #175

Closed binoculars closed 10 years ago

binoculars commented 10 years ago

I'd like the iteration in template repeat to be more intuitive.

Take for instance,

var arrayOfObjects = [{'value': 'a'}, {'value': 'b'}, {'value': 'c'}];
var arrayOfStrings = ['a', 'b', 'c'];
var arrayOfIntegers = [1, 2, 3];

This seems to work just fine:

<ul>
    <template repeat="{{ arrayOfObjects }}">
        <li>{{ value }}</li>
    </template>
</ul>

However, the following do not work:

<ul>
    <template repeat="{{ myStr in arrayOfStrings }}">
        <li>{{ myStr }}</li>
    </template>
</ul>

<ul>
    <template repeat="{{ myInt in arrayOfIntegers }}">
        <li>{{ myInt }}</li>
    </template>
</ul>

This will print test twice.

<ul>
    <template repeat="{{ arrayOfStrings }}">
        <li>test</li>
    </template>
</ul>
sjmiles commented 10 years ago

Fails:

<ul>
    <template repeat="{{str in arrayOfStrings}}">
        <li>{{str}}</li>
    </template>
</ul>

Works:

<ul>
    <template repeat="{{arrayOfStrings}}">
        <li>{{}}</li>
    </template>
</ul>

http://jsbin.com/hurata/1/edit

ajklein commented 10 years ago

Isn't this just because the foo in foos syntax is part of polymer-expressions, not vanilla TemplateBinding? http://jsbin.com/qolisemi/1/edit (wrapping the whole thing in an element) works fine.

sjmiles commented 10 years ago

Doh!

sjmiles commented 10 years ago

Sorry, I completely messed this up.

@binoculars, your example is not parallel. In the first case, you are binding to an object and then plucking out a value from inside. The parallel constructions are:

    <template repeat="{{ arrayOfObjects }}">
        <li>{{ value }}</li>
    </template>

    <template repeat="{{ arrayOfStrings }}">
        <li>{{}}</li>
    </template>

    <template repeat="{{ arrayOfIntegers }}">
        <li>{{}}</li>
    </template>

In all cases, {{}} represents the object bound to the iteration.

As @ajklein correctly points out, the aliasing syntax is an extra provided by the polymer-expressions library.

http://jsbin.com/hurata/4/edit

binoculars commented 10 years ago

You're absolutely correct. I wasn't aware of {{}}. I think you can see where the confusion is happening though--maybe something to improve on? Not sure if it would be possible or in keeping with best practices of some features in Polymer Expressions were available by default TemplateBinding.

sjmiles commented 10 years ago

Yes, we see where the confusion is happening.

TemplateBinding being separated from additional expression logic is a good design, however it would clearly be useful to have some kind of bundle with polymer-expressions, especially since the latter does not have any actual dependencies on Polymer. You should be able to load both libs without too much trouble.

Fwiw, we are focused on Polymer as a package including the data-binding system, and although we have structured things such that it's possible to use the pieces individually, as a survival mechanism we aren't providing much help with doing that. We'll see how things evolve.

Thanks for the ticket!