polymer icon indicating copy to clipboard operation
polymer copied to clipboard

Element class binding inside nested templates returns undefined

Open chimarcos opened this issue 11 years ago • 2 comments

Consider the following code:

<dom-module id="x-foo">
    <template>
        <template id="captionTemplate">
            <i class$="{{headerIcon}}"></i>
            <span>{{headerCaption}}</span>
        </template>

        <div class="portlet light">
            <i class$="{{headerIcon}}"></i>
            <div class="portlet-title">
                <div class="caption"></div>
            </div>
            <div class="portlet-body">
                <form class="form-body">
                    <content></content>
                </form>
            </div>
        </div>
    </template>

    <script>

        var
            getCaptionTemplate
        ;

        getCaptionTemplate = function() {
            var clientTemplate = $( this ).find( "template[portlet-caption-template]" );
            var template = $( this ).find( "#captionTemplate" )[ 0 ];

            if ( clientTemplate.length ) {
                template.remove();
                template = clientTemplate[ 0 ];
            }

            return template;
        };

        Polymer( {

            is: "x-foo",

            properties: {
                headerCaption: String,
                headerIcon: String
            },

            behaviors: [ Polymer.Templatizer ],

            ready: function() {
                var
                    captionTemplate
                ;

                captionTemplate = getCaptionTemplate.call( this );

                captionTemplate.remove();

                this.templatize( captionTemplate );

                var captionTemplateInstance = this.stamp();

                captionTemplateInstance.headerIcon = this.headerIcon;
                captionTemplateInstance.headerCaption = this.headerCaption;

                $( this ).find( ".portlet-title > .caption" ).append( captionTemplateInstance.root );
            }
        } );
    </script>
</dom-module>

When I call this.templatize( captionTemplate ) the class attribute of the <i> element inside of the nested template wouldn't get the bound property value. This happens because when assigned as a behavior, the Polymer.Templatizer overrides _scopeElementClass to _scopeElementClassImpl and checks if there is a _rootDataHost to return the scoped classes. But I think there's an else missing there. If there's no host, return the value itself since it's a very specific css class speficied by the user.

_scopeElementClassImpl: function(node, value) {
        var host = this._rootDataHost;
        if (host) {
            return host._scopeElementClass(node, value);
        } else {
            return value;
        }
    }

chimarcos avatar Sep 11 '15 20:09 chimarcos

I'm not sure why you would construct your element this way, and I'm not sure I can follow your description.

Can you make a JSBin example?

dfreedm avatar Nov 11 '15 18:11 dfreedm

Closing due to age, please reopen if this is still an issue.

dfreedm avatar Jan 28 '16 20:01 dfreedm