Creating a polymer element via dart code

I am trying to instantiate an element Polymer

in my Dart code.

Here's my element:

@CustomTag('card-component')
class CardComponent extends PolymerElement {
  @published
  String playerName;

  CardComponent.created() : super.created();
}

      

So far, the only way I've worked with was:

Element y = new Element.tag('card-component');

      

But I would like to access the values ​​of my component through their accessors (and not by attributes HtmlElement

). Is there a way to create an instance CardComponent

directly in Dart without using a method Element.tag

? I tried using the method in CardComponent.created()

multiple ways, but it didn't work either.

If you are wondering why I want to do this, I would like to pass parameters to my constructor, or even use a factory to create a component in a perfect world.

Edit: After reading this answer , I ended up being able to do what I wanted. I just had to do this with my component

factory CardComponent(String playerName) => document.createElement("card-component")
  ..attributes = {
    "playerName" : playerName
};

      

And then create it like this:

CardComponent myNewCard = new CardComponent("Pacane");

      

+3


source to share


1 answer


I don't know what exactly you mean by "to access the values ​​of my component by their accessories (not attributes HtmlElement

)"

(new Element.tag('card-component') as CardComponent)
    ..playerName = 'blabla';

      

Adding a custom factory constructor allows the element to be used as if it had a normal constructor



@CustomTag('card-component')
class CardComponent extends PolymerElement {
  @published
  String playerName;

  CardComponent.created() : super.created();

  factory CardComponent CardComponent() {
    return new Element.tag('card-component');
  }
}

      

var cc = new CardComponent()..playerName = 'blabla';

      

If you want to do this because main()

not from within another component, make sure that main()

contains polymer (initialization code for some reason I get an error type polymer.dart element? , How to implement the basic function in polymer applications )

+4


source







All Articles