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

后端 未结 2 1505
萌比男神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: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

    
      
      
    
    

    Or if you want to have some default elements:

    
      
      
    
    

    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:

提交回复
热议问题