googlearchive / TemplateBinding

TemplateBinding Prolyfill
290 stars 61 forks source link

Dynamically updating ref not working as expected #151

Closed wereHamster closed 10 years ago

wereHamster commented 10 years ago

<div>
    <template id="external">success</template>

    <template id="t" bind>
        <template bind ref="{{ ref }}"></template>
    </template>
<div>

<script>
    var t = document.getElementById('t');
    t.model = { ref: '' };

    setTimeout(function() {
        t.model.ref = 'external';
    }, 0)
</script>

I expect to see 'success' but nothing appear on the page. TemplateBinding only re-renders the template when bind, repeat or if change. But there doesn't seem to be a corresponding observer on the ref attribute.

rafaelw commented 10 years ago

This is as designed. Updating the ref (either directly or via binding) won't affect existing instances (i.e. "re-render"). If this was working before, it was accidental. I understand this might be convenient, but it opens a box of worms we're not prepared to deal with.

wereHamster commented 10 years ago

There is an ugly workaround for that problem, but I'd still prefer if it TemplateBinding did this automatically.

<polymer-element name="template-ref" attributes="ref content">
    <template>
        <template ref="{{ ref }}" bind="{{ m }}"></template>
    </template>
    <script>
        Polymer("template-ref", {
            refChanged: function() {
                // Create a new object so that polymer re-renders the template
                // with the new ref.
                this.m = { content: this.content }
            }
        });
    </script>
</polymer-element>
rafaelw commented 10 years ago

Reconsidering this...

rafaelw commented 10 years ago

So I think the behavior of a bound ref would be that when/if the referenced template changes, all instances are destroyed and new instances are created using the newly referenced template content

rafaelw commented 10 years ago

Ok. This is now implemented as of: https://github.com/Polymer/TemplateBinding/commit/2799341