Rendering a content tag as part of a template in polymer and dart

后端 未结 2 1506
萌比男神i
萌比男神i 2021-01-04 10:26

I wish to make a generic list using polymer and dart. I am extending the UL element to do so. I want to place template variables within the content of this custom element.<

相关标签:
2条回答
  • 2021-01-04 11:27

    Unfortunately, there are two issues here.

    1. <content> cannot be used like this. It's a placeholder for rendering light DOM nodes at specific locations in the Shadow DOM. The first <content> that selects nodes, wins [1]. Stamping out a bunch like you're doing, while very intuitive, won't work as expected.

    2. You're mixing the internal world of Polymer with the external world outside the element. What this really means is that bindings (e.g. {{}}) only work in the context of <polymer-element>.

    One thing you can do is create a copy of the distributed light DOM children as the items property of your element. In JavaScript this looks like:

    <template repeat="{{item in items}}">
      <li>{{item['first_name']}}</li>
    </template>
    <content id="content" select="li"></content>
    <script>
      Polymer('data-ul', {
        ready: function() {
          this.items = this.$.content.getDistributedNodes();
        }
      });
    </script>
    

    Note: The only reason I've used <content select="li"> is to insure the element only takes in <li> nodes. If you're not worried about users using other types of elements, just use this.items = [].slice.call(this.children);.

    0 讨论(0)
  • 2021-01-04 11:28

    To do that you should override the parseDeclaration method. This method is in charge of parsing/creating the needed html that will be bound. For example, let say that you have next template

    <polymer-element name="data-ul" extends="ul" attributes="items">
      <template>
        <template repeat="{{item in items}}" ref="itemTemplate"></template> <!-- this is the replacement of content tag -->
      </template>
      <script type="application/dart" src="data-ul.dart"></script>
    </polymer-element>
    

    Or if you want to have some default elements:

    <polymer-element name="data-ul" extends="ul" attributes="items">
      <template>
        <template repeat="{{item in items}}">
          <!-- Def elements -->
          <template bind="{{item}}" ref="itemTemplate"></template> <!-- this is the replacement of content tag -->  
          <!-- Def elements -->
        </template>
      </template>
      <script type="application/dart" src="data-ul.dart"></script>
    </polymer-element>
    

    then you should have next class:

    @CustomTag('data-ul')
    class DataUl extends LiElement with Polymer, Observable {
      DataUl.created() : super.created();
    
      @published List items;
    
      void parseDeclaration(Element elementElement) {
        // We need to remove previous template from element.templateContent
        // in that way it no continues adding a new content every time that we instantiate 
        // this component.
        var previousTemplate = element.templateContent.querySelector('template#item');
        if(previousTemplate != null)
          previousTemplate.remove();
    
        var t = this.querySelector('#itemTemplate'); // Gets the template with id itemTemplate from the content html
        if(t != null)  // if not null
          element.templateContent.append(t); // append itemTemplate to element.templateContent
        else 
          element.templateContent.append(new TemplateElement()..id='itemTemplate'); //if no template is added append an empty template to avoid errors
    
    
        super.parseDeclaration(elementElement); // call super
      }
    }
    

    And finally use the custom element as follow:

    <ul is="data-ul" items="{{[{'first_name': 'jay'}, {'first_name': 'joy'}]}}">
      <template id="itemTemplate">
        <li>{{item['first_name']}}</li>
      </template>
    </ul>
    
    0 讨论(0)
提交回复
热议问题